Python - do I need to learn about old style classes?

445 views Asked by At

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?

2

There are 2 answers

0
Daniel DiPaolo On BEST ANSWER

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!

0
Daniel Roseman On

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...)