Main Content

Writing Python 2 such that Python 3 is easy

Archive - Originally posted on "The Horse's Mouth" - 2016-11-06 14:13:33 - Graham Ellis

If you're writing code in Python 2 (2.7.x) ... and want an easy port to Python 3

a) Use the format method in all your prints - example ([here]

b) Use // if you're dividing and want an integer result

c) Wrap standard functions that you need to return a list in list( through )

d) used sorted in preference to sort

e) Inherit lowest base classes from object even though not needed in Python 3

f) test the code in Python 3 occasionally ... at a unit test level even if not the whole application!