views:

304

answers:

3

I am learning to program in Python and we have version 2.7 installed at work. Whenever I try to dive deep into Python, I never liked the dea of diving into a deprecated version (2.7). My work is not ready for 3.0 just yet.

My question is : I want to write code in 2.7 which can easily be converted to 3.0 using 2to3. Honestly, I want to style my 2.7 code in a 3.0 manner.

Any helpful pointers, books that exist to achieve this style? I prefer the cutting edge features of 3.0 by not compromising my 2.7 knowledge required at work. Possible?

I really appreciate your answers (:

+4  A: 

Make use of the __future__ module. The more you make use of, the easier it will be to port later. In particular I'm a fan of division and print_function, these improve Python semantics even if Python 3k isn't in mind.

Other compatibility practises include using iterators wherever you can, and avoiding deprecated methods and functions on core types.

A final trick I like to use is to import the standard library modules with their new names, as this removes the need to go and update references to the module everywhere they're used later on, or provide nasty backwards compatibility hacks (also the new names are much more pleasant):

import Tkinter as tkinter
Matt Joiner
??? Far from being deprecated, py2's `iteritems` is essentially the semantic equivalent of py3's `items` and 2to3 will of course rename it without any problems whatsoever. Indeed, `d.items()` in py2 will be translated by 2to3 into `list(d.items())`! It really makes no sense to avoid perfectly useful methods, with just the right semantics _and_ easily translatable.
Alex Martelli
Oh I didn't mean to stop using them, just be ready to change the names. Perhaps I should emphasise this.
Matt Joiner
@Matt Joiner: Change the names?? Don't you mean "be ready to run 2to3"?
John Machin
+13  A: 

3.0 is totally obsolete and should be avoided like the plague; the current version of Python 3 is 3.1, and soon there will be a 3.2. Please have nothing to do with 3.0!

2.7 is easily written in a way that helps 2to3 rewrite the code easily for 3.x -- Mark Pilgrim explains the details well, so I guess you could say that his "Dive Into Python 3" online book come reasonably close to meeting your request (and is also easy to check out, since it is a free online book;-).

Alex Martelli
Are you also emphatically against people using Python 2.6 (or 2.5)?
Nick T
@Nick, absolutely not -- 2.6 is still (I think) fractionally more solid, robust, and reliable than 2.7 (which after all hasn't even had a 2.7.1 bugfix release yet) so it's 100% reasonable to stick with it for production work. 2.5, while old, is still a must for such crucial uses as Google App Engine (and possibly other "Python embedded in specific apps" uses) so there's plenty of motivation to keep knowing it and in some case using it. 3.0 was a transient, experimental version, without any advantages wrt 3.1, and as far as I know it's "embedded" absolutely nowhere -- NO reason to use it.
Alex Martelli
+6  A: 

You can have Python 2.7 use many of the features of Python 3+ with

from __future__ import division, print_function, unicode_literals
from future_builtins import *  # map(), etc.

With this, 3/2 yields 1.5 (and not 1), printing is done with print() (instead of print …), and all string literals are unicode strings (instead of "byte" strings). You also have the new map(), which behaves like itertools.imap(), etc.

PS: Using unicode_literals makes all your string literals (like "Hello") represent Unicode objects, which are essentially equivalent to Python 3's strings. However, using this option can force you to code differently than in Python 3, because you might have to perform some encodings/decodings that Python 3 would not require. Thus, depending on your needs, it might be a good idea to not use unicode_literals.

EOL