new-style class or old-style class?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Sep 25 12:20:44 EDT 2012


On Tue, 25 Sep 2012 07:44:04 -0700, Jayden wrote:

> In learning Python, I found there are two types of classes? Which one
> are widely used in new Python code? 

New-style classes.

> Is the new-style much better than old-style?

Yes.

Always use new-style classes, unless you have some specific reason for 
needing old-style ("classic") classes.

Advantages of new-style classes:

1) They are the future. In Python 3, all classes are "new-style" and 
classic classes are gone.

2) Multiple inheritance works correctly. Multiple inheritance for classic 
classes is buggy.

3) New-style classes support awesome features like super(), properties, 
descriptors, and __getattribute__. Old-style do not.

The main disadvantage is that automatic delegation is a pain to do 
correctly in new-style classes, but trivially simple in classic classes. 
Still, all things considered, it's a good trade.



-- 
Steven



More information about the Python-list mailing list