A project that I’m working on is being produced with the intention that it will be in production for a very long time — 10+ years. Thus converting from Python 2.7 to Python 3.4 seems to be a wise option to explore.
These are some personal notes regarding the differences between the two versions, supplementing the excellent articles out there. These are based on examining diffs after running 2to3 on the code base.
xrange and range
Contrary to what is implied in many articles, the separate functionality in xrange and range has not been mysteriously merged.The Python 2.7 xrange has been renamed to range. The Python 2.7 range is gone completely. To get the same effect, use
foo = list(range(1, 10))
The 2to3 code tool did this correctly; seeing its effect merely clarified how Python 3 works.
I had to manually go through my objects and straighten out __str__ and __unicode__. This involved:
- Deleting __str__ methods which essentially duplicated __unicode__.
- Renaming __unicode__ to __str__.
I used __long__ in one object, and simply deleted it since under Python 3 it is a duplicate of __int__.
long() and int()
Similar to __long__, there was one instance where I simply deleted the code associated with long().
I had to manually convert the Python 2.7
2to3 attempts to convert map() statements to list comprehensions. It did well on most, but screwed up in two places. It might be wise to search for leftover map() statements.
- 14 May 2014
- Added notes for map().