Backwards Compatibility of Python versions

Neal Norwitz neal at metaslash.com
Mon Feb 4 18:30:44 EST 2002


Paul Rubin wrote:
> The issue is there's no way to
> use integer division that works in both 2.1 and 3.0, without using
> something like divmod that would shock the unwary.

#from __future__ import division # uncomment this line if in 2.2+

def divide(n, d): 
    if type(n) == type(d) == type(0):
        return int(n / d)
    return n / d 

print divide(5.3, 2.3)
print divide(5, 2)
print divide(4, 2)

[neal at epoch neal]$ python1.5 /tmp/div.py 
2.30434782609
2
2
[neal at epoch neal]$ python2.1 /tmp/div.py 
2.30434782609
2
2
[neal at epoch neal]$ python2.2 /tmp/div.py 
2.30434782609
2
2
[neal at epoch neal]$ python2.2 /tmp/div.py # with from __future__ line uncommented
2.30434782609
2
2

So unless I'm missing something, this is what you want. 
And the .pyc is only 406 bytes. :-)

Neal



More information about the Python-list mailing list