Python 2 to 3…

Not a python person (used it in some projects, but it’s not my main tool). That said, I’ve been watching the “2->3” migration horror stories and woes for several years. In 2014, the “2015 deadline” for Python 2 deprecation was pushed back to Jan 1, 2020, so it’s once again starting to loom large.

I came across this little gem which may be one reason why so many people didn’t try to upgrade to 3 from 2…

The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.round([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.

So any code you had that relied on ’round’ function in Python 2 would automatically work differently when moving to 3. That may be a lot of code to audit/test/change!

(Edit: have learned this pattern is “banker’s rounding“. Still not sure it warranted such a change (or why the algorithm wasn’t pointed out as “banker’s rounding” in the change docs I’d found).

Similarly, “print” was changed from a statement to a function.

2.x: print "The answer is", 22 
3.x: print("The answer is", 22)

The 2.x style simply doesn’t work in Python 3. Again, that would be a hell of a lot of code to review/audit/upgrade, for no functional benefit.

Have you made the jump to 3? Did you just start with 3? Did you have to do a migration? Was it as bad as it sounds?

Leave a Reply

Your email address will not be published. Required fields are marked *