How to learn Python 2 if I already know Python 3?

I have some knowledge of Python 3 (I'm not a newbie, but I'm not an expert). I'm interested in web development, so I want to use Django. What are the differences between the two versions of Python? How to switch from 3 to 2.x?

+7
source share
5 answers

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.

+5
source

If you are already familiar with Python 3, then there are almost no differences that you have to worry about when coding in Python 2. The most distinguishable differences for the user are related to the details of the print statement, which you probably won't use for Django.

So, just write the code and ask about any specific problems you may encounter.

+5
source

Another big difference is how Python 3 handles unicode - everything in Python 3 is either a unicode string or binary data, while in Python 2 a distinction was made between Unicode strings and 8-bit strings.

The next page has a lot more information about the difference between Python 2 and 3. http://docs.python.org/release/3.0.1/whatsnew/3.0.html

+3
source

Read this: http://python3porting.com/differences.html

Note that there are many things that are simply removed from Python 2, such as apply (), so you have nothing to worry about.

In addition, as noted by senderle, you are using a subclass from the object (this is also recommended in Python 3, perhaps because it really affects Python 2).

+1
source
0
source

All Articles