What is the difference between old style and new style classes in Python? Is there ever a reason to use old-style classes these days?
Join them; it only takes a minute:
|
From http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes :
|
|||||||||||||||||||||
|
|
Declaration-wise: New-style classes inherit from object, or from another new-style class.
Old-style classes don't.
|
|||||||||||||||||||||
|
|
Important behavior changes between old and new style classes
MRO (Method Resolution Order) changedIt was mentioned in other answers, but here goes a concrete example of the difference between classic MRO and C3 MRO (used in new style classes). The question is the order in which attributes (which include methods and member variables) are searched for in multiple inheritance. Classic classes do a depth first search from left to right. Stop on first match. They do not have the
New-style classes MRO is more complicated to synthesize in a single English sentence. It is explained in detail here. One of its properties is that a Base class is only searched for once all its Derived classes have been. They have the
New style class objects cannot be raised unless derived from
|
|
Nice clear summary, thanks. When you say "difficult to explain in English" I think you are describing a postorder depth-first search as opposed to the old-style class which uses a preorder depth-first search. (preorder means we search ourself before our first child and postorder means we search ourself after our last child).
– Steve Carter
Oct 5 '16 at 9:20
|
|
Old style classes are still marginally faster for attribute lookup. This is not usually important, but may be useful in performance-sensitive Python 2.x code: In [3]: class A: ...: def __init__(self): ...: self.a = 'hi there' ...: In [4]: class B(object): ...: def __init__(self): ...: self.a = 'hi there' ...: In [6]: aobj = A() In [7]: bobj = B() In [8]: %timeit aobj.a 10000000 loops, best of 3: 78.7 ns per loop In [10]: %timeit bobj.a 10000000 loops, best of 3: 86.9 ns per loop |
|||||||||||||||||||||
|
|
Guido has written The Inside Story on New-Style Classes, a really great article about new-style and old-style class in Python. Python 3 has only new-style class, even if you write an 'old-style class', it is implicitly derived from New-style classes have some advanced features lacking in old-style classes, such as |
||||
|
|
|
Here's a very practical, True/False difference. The only difference between the two versions of the following code is that in the second version Person inherits from object. Other than that the two versions are identical, but with different results : 1) old-style classes
2) new-style classes
|
|||||||||||||||||||||
|
|
New-style classes inherit from Read descrintro for more details. |
||||
|
|
|
Or rather, you should always use new-style classes, unless you have code that needs to work with versions of Python older than 2.2. |
|||
|
|
|
New style classes may use
And in Python 3.x you can simply use |
|||
|
|
super? – accuya Dec 25 '12 at 2:13Aand in each one you call a method of the base class explicitly, usingA.some_method, then you will have to change every call in the 10 base classes. – Phillip Cloud Dec 27 '12 at 20:04