5

I am currently learning Python 2.6.5 and I found out about old style classes and new style classes.

I understand that these classes are still existing only for backward compatibility and that they are removed in Python 3.

So the question is this: as a newcomer to the language, do I need to learn about the classic classes?

P.S. I am learning Python 2 because Python 3 is still not fully supported in frameworks and I want to learn some frameworks too. The plan will be to move to Python 3 when frameworks catch up, but until then, do I need to worry about the old style classes?

0

2 Answers 2

13

No. Don't bother. Simply inherit all your classes from object (or from classes that inherit from object) and you will be good to go. Then when you transition to Python 3 you can forget that bit of syntax.

There's no advantage to using or even learning about old-style classes at this point.

So just make sure all of your class declarations look like this:

class foo(object):
    ...

(or inherit from something other than object which does inherit from object), and then pretend this is the way that it has always been!

Sign up to request clarification or add additional context in comments.

Comments

4

Although I agree with Daniel DiPaolo that you should never need to use old-style classes in your code, there will be times when you will need to understand them a little bit.

For example, with old-style classes you can't use super to call a parent method - and this can bite you when you try and subclass, for example, the urllib2.Request class. If you do this without realising you'll get the cryptic error super() argument 1 must be type, not classobj, which can take ages to debug.

(As you can probably tell, I've been there...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.