Main Content

A reminder of the key issues to consider in moving from Python 2 to Python 3

Archive - Originally posted on "The Horse's Mouth" - 2016-10-30 10:50:20 - Graham Ellis

1. New style classes take on the old style class format
  But does it matter if you explicity inherit in Python 3??

2. print moves from being a keyword to a function
  Single value tuple as parameter will work in both cases
  You may also do a sys.stdout.write

3. integer division using "/" returns a float
  Use // if you need the whole number back

4. raw_input becomes input
  Thank goodness ;-)

5. sort's callback changes to a "key =" type using a single parametered lambda
  But remember you have things like sorted backported to 2.7

6. Text strings défaut to unicode in Python 3

7. You need all your modules and dependencies sorted out for Python 3 before you switch to that

8. You can't use new Python 3 stuff in Python 2 unless it's been backportd

In most cases, it's practical to write code that works with both Python2 and Python 3. However, if you must write something a bit different, you can use:
  if sys.version_info < (3,0):
and you do need to remember that even if you do this, the syntax must be compatible with both the Python 2 and Python 3 compilers!