The new class of python is introduced in version 22. we can call the previous class a classic class or an old class. Why do we need to introduce newstyleclass in 22? The official explanation is: the new class of python is introduced in version 2.2. we can refer to the previous class as a classic class or an old class.
Why should we introduce new style class in 2.2? The official explanation is:
To unify the class and type ).
Before MySQL 2.2, for example, in MySQL 2.1, classes and types are different. if a is an instance of ClassA,. _ class _ return 'class _ main __. classA ', type (a) always returns . After new classes are introduced, for example, ClassB is a new class, B is an instance of ClassB, and B. both _ class _ and type (B) return 'class' _ main __. classB.
After a new class is introduced, there are other benefits, such as more built-in attributes, descriptor introduction, and attribute calculation.
To forward compatibility, the user-defined class is a classic class by default. The new class must inherit the base class object of all classes or the new class of the object.
It is worth noting that although the latest python (2.7) is used, some features will not work in the old class.
Therefore, there are two ways to ensure that you are using a new class:
1. Meta-class: add the following code _ metaclass _ = classname (a new custom class) at the beginning of the class module code ).
2. classes are inherited directly or indirectly from built-in class objects.
If you do not need to be compatible with old-style classes or earlier-version classes, the new classes will remain the same.
In Python3, these problems do not exist because all classes are subclasses of the object class (implicit ).
Sample code:
Class oldClass: # Classic class def _ init _ (self): pass class newClass (object): # New class def _ init _ (self): pass c1 = oldClass () c2 = newClass () c1. _ class _ # output->
Type (c1) # output->
C2. _ class _ # output->
Type (c2) # output->
The above is a detailed explanation of the differences between the new and old types of python. For more information, see other related articles in the first PHP community!