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?
From http://docs.python.org/ref/node33.html :
Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance of an old-style class, then x.
__class__
designates the class of x, but type(x) is always < type 'instance'>. This reflects the fact that all old-style instances, independently of their class, are implemented with a single built-in type, called instance.New-style classes were introduced in Python 2.2 to unify classes and types. A new-style class neither more nor less than a user-defined type. If x is an instance of a new-style class, then type(x) is the same as x.
__class__
.The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model. It also has a number of immediate benefits, like the ability to subclass most built-in types, or the introduction of "descriptors", which enable computed properties.
For compatibility reasons, classes are still old-style by default. New-style classes are created by specifying another new-style class (i.e. a type) as a parent class, or the "top-level type" object if no other parent is needed. The behaviour of new-style classes differs from that of old-style classes in a number of important details in addition to what type returns. Some of these changes are fundamental to the new object model, like the way special methods are invoked. Others are "fixes" that could not be implemented before for compatibility concerns, like the method resolution order in case of multiple inheritance.
The plan is to eventually drop old-style classes, leaving only the semantics of new-style classes. This change will probably only be feasible in Python 3.0. new-style classic old-style
The short answer: old-style classes are only there for backwards compatibility, and will go away in Python 3.0. You should always use new-style classes.
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 inherit from object and must be written as such in Python 2.2 onwards (i.e. 'class Classname(object):' instead of 'class Classname:'). The core change is to unify types and classes, and the nice side-effect of this is that it allows you to inherit from built-in types.
Read descrintro for more details.
In the first answer, there is a missing "is":
A new-style class neither more nor less
should be:
A new-style class is neither more nor less
Declaration-wise:
New-style classes inherit from object.
class NewStyleClass(object):
pass
Old-style classes don't.
class OldStyleClass():
pass
Hey Guy's
1) What about mro (very important) 2) super support in new class types 3) and as i can recall new is also introduced in new type of classes
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