Differences between new and old classes in python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.