user3316811
user3316811

Reputation:

Python 2 to 3 issue warnings

I recently came across some pretty "shy" bugs from running python2 code with python3. The reason was automatic conversion of integer division to float. So even though I didn't do this:

from __future__ import division

my code could run, the wrong way though.

I'm aware of 2to3 but the issues are the following:

Thanks in advance for your time, feedback on any of the questions will be very helpful.

Upvotes: 1

Views: 132

Answers (1)

solusipse
solusipse

Reputation: 587

Python 3 by default implements behavior of division module from __future__.

Python 2:

>>> 1.0 / 2.0
0.5
>>> 1/2
0
>>> from __future__ import division
>>> 1.0 / 2.0
0.5
>>> 1/2
0.5

Python 3:

>>> 1.0 / 2.0
0.5
>>> 1/2
0.5

To achieve behavior characteristic for version 2 use // operator:

>>> 1//2
0
>>> 1.0 // 2.0
0.0

Upvotes: 1

Related Questions