Convering from Python 2 to Python 3 - an update, and the 2to3 utility
Archive - Originally posted on "The Horse's Mouth" - 2016-10-30 10:15:54 - Graham EllisPython 3 has now been with us for a number of years, but the move across from Python 2 to Python 3 is still far from complete. And as Python 3 is not source code compatible with Python 2, that's no big surprise to anyone. The very good reason for the changes is that the correct decisions made on langauge syntax in 1988 would not be the correct decisions for the language for 2018 or 2028, but there's a huge user bas of code out there which people have invested in and need to maintain and keep running, and a huge number of modules and packages that need to be converted and tested before end user programmers can make full use of them.
In order to help with the move in the future - so that Python 2 support can eventually (2019?) be phased out, some of the new facilities have been backported to Python 2. The new format method on a string, for example, and the with keyword. But some things cannot be easily backported.
Recommmendation is that you stick with developmement in 2.7 while you are still supporting Python 2 strema machines, writing your code with an eye on Python 3. It is possible to write code that will compile and run in both - our json parser demo is an example - but there are time that's not the optimum thing to do.
Supplied with Python3 is a utility called 2to3 which will do a lot of the work for you; be wary though, because there are some changes in a dynamic language like Python which it's simply not possible to automate.
Here's an example of 2to3 in use:
WomanWithCat:q2 grahamellis$ 2to3 -w dx
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored dx
--- dx (original)
+++ dx (refactored)
@@ -19,9 +19,9 @@
edges = getEdges(width,height,depth)
corners = getCorners(width,height,depth)
-print "Our",what,"is",width,"by",height,"by",depth
-print "Volume is",volume
-print "Surface ares is",surface
-print "Edge length is",edges
-print "Corners",corners
+print("Our",what,"is",width,"by",height,"by",depth)
+print("Volume is",volume)
+print("Surface ares is",surface)
+print("Edge length is",edges)
+print("Corners",corners)
RefactoringTool: Files that were modified:
RefactoringTool: dx
And here's a note of the changes it's made:
WomanWithCat:q2 grahamellis$ diff dx dx.bak
22,26c22,26
< print("Our",what,"is",width,"by",height,"by",depth)
< print("Volume is",volume)
< print("Surface ares is",surface)
< print("Edge length is",edges)
< print("Corners",corners)
---
> print "Our",what,"is",width,"by",height,"by",depth
> print "Volume is",volume
> print "Surface ares is",surface
> print "Edge length is",edges
> print "Corners",corners
WomanWithCat:q2 grahamellis$
Our public Python Courses are moving from Python 2 to Python 3 over the next year; examples are run in both at present and that will continue, but the bias will switch - and with typical group sizes being just three or four delegates, I'm well able to tailor the presentation to suit the group.
Private courses are run using Python 2, or Python 3, after a discussion with the technical lead prior to the course. Almost inevitably, any courses in Python 2 will have a strong element of "do it this way to prepare for the future" about them, but it's rare for a course to be jumping back and forth between every example as that serves more to baffle than to clarify!