top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Integer division debugging in Python

+1 vote

The change in integer division seems to be the most insidious source of silent errors in porting code from python2 - since it changes the behavior or valid code silently.

I wish the interpreter had an instrumented mode to detect and report such problems.

posted Aug 28, 2013 by Satish Mishra

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote
Best answer

Run the code under Python 2.6/2.7 with the -3 flag:

$ cat 
print(10 / 7)

$ python -3 DeprecationWarning: classic int division
 print(10 / 7)
answer Aug 28, 2013 by Bob Wise
Similar Questions
+2 votes

I wanna simulate C style integer division in Python3.

So far what I've got is:

# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
 result = math.floor(result)
 result = math.ceil(result)

I found it's too laborious. Any quick way?

0 votes

I've written a program using Twisted that uses SqlAlchemy to access a database using threads.deferToThread(...) and SqlAlchemy's scoped_session(...). This program runs for a long time, but leaks memory slowly to the point of needing to be restarted. I don't know that the SqlAlchemy/threads thing is the problem, but thought I'd make you aware of it.

Anyway, my real question is how to go about debugging memory leak problems in Python, particularly for a long running server process written with Twisted. I'm not sure how to use heapy or guppy, and objgraph doesn't tell me enough to locate the problem. If anyone as any suggestions or pointers it would be very much appreciated!

+2 votes

I am using the Linux system with python, i am running the following script


import threading

import time

import sys
import subprocess
import datetime
import os
import time
import logging


In this script i am calling the other script named which is located on the desktop with 64 and abc as its arguments I am getting the following error

File "/usr/lib64/python2.6/", line 589, in __init__
 raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

Can someone help me?

0 votes

I was following an exercise in a book when I edited the code and came across something I did not get. Here is the relevant part of the code that works:

start=None #initialise 
while start !="": 
 start=input("nStart: ")

 if start:
 finish=int(input("Finish: ")) 

 print("word[",start,":",finish,"] is", word[start:finish])

I then changed the code to this:

start=None #initialise 
while start !="": 
 start=int(input("nStart: "))

 if start:

 finish=int(input("Finish: "))

 print("word[",start,":",finish,"] is", word[start:finish])

I combined the int conversion and the input on the same line, rather than to have two different statements. But got an error message:

Traceback (most recent call last):
 File "", line 23, in 
 start=int(input("nStart: "))
ValueError: invalid literal for int() with base 10: ''

Could someone tell me why I got this error message?

0 votes

Inputs would be:
- Name
- age
- gender
- married/unmarried

User feeds this data once but he/she can check later on also. So in this case program should have option to feed and read data from file.

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.