Skip to content

python3 transition

Eric Vaandering edited this page Mar 9, 2017 · 18 revisions

This link https://docs.python.org/3/howto/pyporting.html has a lot of good stuff on all the differences between python 2 and 3. The focus is on writing code that works in python 2.6, 2.7, and 3.4 and above. In particular part of our plan will be to pass code through python-futurize http://python-future.org/automatic_conversion.html . Developers should consider doing this now when changing existing code and validating unit tests.

items(), keys(), and values()

Python2 uses these keywords to and creates lists of them. This can take a lot of memory. Python3 has the same syntax, but they are now iterators. In Python2, iteritems(), iterkeys(), and itervalues() behave the same as the Python3 versions. There is a "problem" with the futurist fixer for these issues in that it

  • Converts python2 uses of items() to list(items()) - not a problem, this is just explicit
  • Converts iteritems() to items()
  • This is OK for python3, but on python2 possibly alters the performance of the code
  • And if you convert this again you now end up with list(items()) in your python3 code altering the performance under Python3 too

Eric's proposal is to use the futurize fixer, but discard all changes that change iteritems() etc to the python 3 versions and use that as the python2 code. We would run it a second time to create a dedicated python3 version. We could also take this opportunity to review our uses of items(), etc in python2 since in most cases we could be using the iterator versions. The most common case where we can't do this is in doing something like len(items()).

Clone this wiki locally