The new style classes in python were introduced in version 2.2, and we can call the previous classes classic or old style classes.
Why introduce new style class in 2.2? The official explanation given is:
To unify classes and types.
Prior to 2.2, e.g., in version 2.1, classes and types were different, e.g., if a was an instance of ClassA, then a.__class__ returned ' class __main__.ClassA' and type(a) returned always <type 'instance ' >. And after introducing a new class, for example, ClassB is a new class, b is an instance of ClassB, b.__class__ and type(b) both return 'class '__main__.ClassB', which is unified.
There are other benefits that come with the introduction of new classes, such as more built-in attributes will be introduced, descriptors are introduced, attributes can come to be computed, and so on.
For forward compatibility, by default user-defined classes are classic classes, and new classes need to inherit from either object, the base class of all classes, or a new class that inherits from object.
It's worth noting that although the latest python (2.7) is being used, some features won't work on old-style classes.
There are two types of classes in Python: old-style classes and new-style classes:
Newer classes all inherit from object, classic classes do not.
The MRO (method resolution order base class search order) algorithm for newer classes uses C3 algorithm breadth-first search, while the MRO algorithm for older classes uses depth-first search
Newer classes execute the constructor only once for the same parent class, classic classes repeat it multiple times.
Among them:
- As of python 2.1, only old-style classes exist. In old-style classes, the class name and type are independent: if x is an old-style class, then x.__class__ defines the class name of x, but type(x) always returns <type 'instance'>. This reflects the fact that all instances of old-style classes are realized through a single built-in type called instance, which is what sets it apart from classes.
- New-style classes were introduced in python 2.2 to unify classes and instances. A new-style class can only be customized by the user. If x is an instance of a new-style class, then type(x) and x.__class__ are the same result (although this is not guaranteed, since the __class__ method of an instance of a new-style class is allowed to be overridden by the user).
- Python's default classes are all classic classes; only explicitly inherited objects are newer classes.
- Python defaults to new-style classes, and classic classes are removed without explicitly inheriting from object.
So, to make sure you are using the new style class, there are two following methods:
1. Metaclass, add the following code to the top of the class module code __metaclass__ = classname (a customized new-style class).
2. Classes are inherited directly or indirectly from the built-in class object.
If you don't need compatibility with old-style classes, old versions of classes, then keep it all new-style.
Inside Python 3, these problems don't exist anymore, because all classes are subclasses of the object class (implicitly).
Code Example:
class oldClass: #Classics def __init__( self ): pass class newClass(object): #New class def __init__( self ): pass c1 = oldClass() c2 = newClass() c1.__class__ # Output-> <class __main__.oldClass at 0x0137BF10> type(c1) # Output-> <type 'instance'> c2.__class__ # Output-><class '__main__.newClass'> type(c2) # Output-><class '__main__.newClass'>
Center:
class A: pass class B: pass class C(B): pass class D(C,A): pass
The order of execution is: D->C->B,->A
class A(object): pass class B(object): pass class C(object): pass class D(A,B,C): pass
The execution order is: D->A->B->C->Object
This is the whole content of this article.