I have to make the Python 2 to 3 jump. Integer division is the only thing that has me scared. Our test suite is OK, but not necessarily set up to scrutinize division. Is there some sort of wrapper or environment or hack from which I could run scripts that can detect when integer division is used? I mostly use Anaconda Python 2.7 on Windows.
You can run your code with the python 2 engine but with -3
switch:
L:\>Python27_X64\python.exe -3 test.py
test.py:1: DeprecationWarning: classic int division
i = 2/3
If you want same behaviour in python 2 or 3, use the
from __future__ import division
at start of each module you're running: now division is float unless you use //
This is a run-time option not a static option so you need good coverage of your code to trap those. For instance it doesn't detect anything on that code:
if False:
i = 2/3
The -3
switch will also detect other incompatibilities (not all of them) without breaking, so you can fix them all at once.
Aside, division is not the hardest issue. The string vs bytes bit is annoying. Consider this:
with open("toto.txt","wb") as f:
f.write("foo")
this is now invalid in Python 3 (strings cannot be written in binary streams), and the -3
script doesn't detect that.