Differences between revisions 2 and 3
Revision 2 as of 2013-08-09 06:51:02
Size: 990
Editor: paddy3118
Comment: Fix headings.
Revision 3 as of 2013-08-09 06:56:37
Size: 997
Editor: paddy3118
Comment: Fix formatting
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
==Why?==;

Seeing [[http://ondrejcertik.blogspot.ch/2013/08/how-to-support-both-python-2-and-3.html|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 [[http://rosettacode.org/wiki/Category:Python|Rosetta Code]] to allow code written in a style to work on both versions of Python interchangeably. 
== Why? ==
Seeing [[http://ondrejcertik.blogspot.ch/2013/08/how-to-support-both-python-2-and-3.html|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 [[http://rosettacode.org/wiki/Category:Python|Rosetta Code]] to allow code written in a style to work on both versions of Python interchangeably.
Line 9: Line 8:
==Simple rules for making code work on both 2.X and 3.Y==; == Simple rules for making code work on both 2.X and 3.Y ==
 1. Use brackets in print statements/functions of one expression.
 1. Use zip and not izip; keys(), values(), items() and not their iter- forms.
 1. Check for raw_input and set raw_input to input if not found.
 1. Conditionally import reduce if it is not found.
Line 11: Line 14:
* 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.

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).

Why?

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

  1. Use brackets in print statements/functions of one expression.
  2. Use zip and not izip; keys(), values(), items() and not their iter- forms.
  3. Check for raw_input and set raw_input to input if not found.
  4. Conditionally import reduce if it is not found.

2and3 One codebase for dual version support (last edited 2013-08-09 06:56:37 by paddy3118)

Unable to edit the page? See the FrontPage for instructions.