[Python-Dev] True division in embedded Python

Tim Peters tim.one@comcast.net
Wed, 29 May 2002 21:42:01 -0400

[Bjorn Pettersen]
> We're currently in the process of adapting Python as the common
> scripting plug-in for all of our projects (and it's working great :-).
> We're standardizing on 2.2.1, and unless there is a pressing business
> need, we'll probably stay with that version for the next 18-24 months.
> My question is if it's intended for users to start using true division
> _now_? The reason I'm inclined to do so is that we're going to have
> snippets of Python code everywhere, including our libraries, disk files,
> and databases -- it would be a major undertaking to find and fix this
> later...

Nevertheless, I recommend you not try to enable true division now.  Part of
the joy of Python is sharing the many libraries and extension modules people
freely provide, and so far nothing except the core distribution is tested
with true division enabled -- and even in the core, true division is tested
sporadically and lightly.  You don't want to be a pioneer here, unless
advancing true division is your business <wink>.

That said, in 2.2.x, you can start using // *now* when you intend flooring
integer division.  That's already implemented.  5/3 will also produce 1 for
now, but don't do it -- do 5//3 instead.

Also read the PEP:


Guido has implemented some quite elaborate mechanisms to help with the
transition, and if you start using i//j now you *should* have a very easy
time when Python 3 changes the rules (indeed, you shouldn't need any
division changes later, provided you use // now).  The simplest of the
mechanisms is -Qwarn:

C:\Python22>type div.py
print 4/3

C:\Python22>python div.py

C:\Python22>python -Qwarn div.py
div.py:1: DeprecationWarning: classic int division
  print 4/3


So, if you like, Python will already warn you about division cases that will
act differently someday.  You can examine them now to determine what you
really intended, and use // now when you intend a truncated result:

C:\Python22>type div2.py
print 4//3

C:\Python22>python -Qwarn div2.py  # no warning produced


Likewise when you intend a floating-point result, you can do something now
to ensure that (spelling it float(i)/j is one easy way).