I have some knowledge of Python 3 (I’m not a beginner, 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 should I switch from 3 to 2.x?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They aren’t so different. Almost everything you learned in Python 3 will transfer to Python 2. I would suggest that you simply dive in. Occasionally you’ll see an error message, but most of the time they’ll be self-explanatory.
My bet is that learning Django will be way harder than getting used to Python 2.
You might find the
sixlibrary helpful if you want to write code that is robustly backwards-compatible. Otherwise, I can only think of two things that might be important to know in advance as you go backwards to Python 2:Avoid using old-style classes. In Python 3, you can declare a class like this, without any problem:
In Python 2, if you do that, you get an old-style class, which you probably don’t want. But you won’t get any error messages about this, so subtle inheritance bugs might arise and stay hidden for a long time before causing problems. So in Python 2, remember to explicitly inherit from
object:Avoid using
range(n), at least for large values ofn. In Python 3,rangereturns an intelligent iterator, but in Python 2,rangereturns an actual list. For large ranges, it can burn up a lot of memory. To get the behavior of Python 3’srangein Python 2, usexrange(n). Similar caveats apply to dictionarykeys(),values(), anditems()methods. They all return lists in Python 2. Use theiterkeys(),itervalues(), anditeritems()methods to save memory.There are several other excellent answers to this question that cover a few other details, such as
unicodesupport.