They are not so different. Almost everything that you learned in Python 3 will be translated into Python 2. I would assume that you are just diving. Sometimes you will see an error message, but most of the time they will be self-evident.
My bet is that learning Django will be a lot harder than getting used to Python 2.
You can find the useful six
library if you want to write reliable reverse code. Otherwise, I can only think of two things that may be important to know in advance when you return to Python 2:
Avoid using old-style classes. In Python 3, you can declare such a class without problems:
class Foo: pass
In Python 2, if you do this, you will get an old-style class that you probably don't want. But you will not receive error messages about this, so subtle inheritance errors may occur and remain hidden for a long time until problems arise. Therefore, in Python 2, be sure to explicitly inherit from object
:
class Foo(object): pass
Avoid using range(n)
, at least for large n
values. In Python 3, range
returns a smart iterator, but in Python 2, range
returns the actual list. For large ranges, it can burn a lot of memory. To get Python 3 range
behavior in Python 2, use xrange(n)
. Similar caveats apply to the dictionary methods keys()
, values()
and items()
. They all return lists in Python 2. Use the iterkeys()
, itervalues()
and iteritems()
to save memory.
There are several other excellent answers to this question that cover several other details, such as unicode
support.
senderle
source share