Skip to content

python3 transition

vilnius-dev edited this page Aug 15, 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()).


The complete list of fixers applied in Stage 2 is:

  1. lib2to3.fixes.fix_basestring

  2. lib2to3.fixes.fix_dict

  3. lib2to3.fixes.fix_exec

  4. lib2to3.fixes.fix_getcwdu

  5. lib2to3.fixes.fix_input

  6. lib2to3.fixes.fix_itertools

  7. lib2to3.fixes.fix_itertools_imports

  8. lib2to3.fixes.fix_filter

  9. lib2to3.fixes.fix_long

  10. lib2to3.fixes.fix_map

  11. lib2to3.fixes.fix_nonzero

  12. lib2to3.fixes.fix_operator

  13. lib2to3.fixes.fix_raw_input

  14. lib2to3.fixes.fix_zip

  15. libfuturize.fixes.fix_cmp

  16. libfuturize.fixes.fix_division

  17. libfuturize.fixes.fix_execfile

  18. libfuturize.fixes.fix_future_builtins

  19. libfuturize.fixes.fix_future_standard_library

  20. libfuturize.fixes.fix_future_standard_library_urllib

  21. libfuturize.fixes.fix_metaclass

  22. libpasteurize.fixes.fix_newstyle

  23. libfuturize.fixes.fix_object

  24. libfuturize.fixes.fix_unicode_keep_u

  25. libfuturize.fixes.fix_xrange_with_import

Clone this wiki locally