Tips for those wishing to create and maintain one Python codebase that works for both Python 2.X and Python 3.Y interpreters (for some value of X and Y).
Seeing one (very good) rant about how hard it is to use the officially blessed way of using 2to3 reminded me that I also ask users on Rosetta Code to allow code written in a style to work on both versions of Python interchangeably.
I thought we needed some central repository giving tips on how to do this and thought I would start with the rules stated on RC.
Simple rules for making code work on both 2.X and 3.Y
- Use brackets in print statements/functions of one expression.
- Use zip and not izip; keys(), values(), items() and not their iter- forms.
- Check for raw_input and set raw_input to input if not found.
- Conditionally import reduce if it is not found.