Python new class and legacy class differences

Source: Internet
Author: User
The new Python class is introduced in version 2.2, and we can call the previous Class A classic or a legacy class.

Why introduce the new style class in 2.2? The official explanation is:

In order to unify classes (class) and types (type).

Before 2.2, for example, in version 2.1, classes and types are different, such as a is an instance of ClassA, then a.__class__ returns ' class __main__. ClassA ', type (a) returns always . When a new class is introduced, such as CLASSB is a new class, B is an instance of CLASSB, and b.__class__ and type (b) Return ' class ' __main__. ClassB ', so it's unified.

With the introduction of new classes, there are other benefits, such as the introduction of more built-in properties, the introduction of descriptors, the ability to calculate attributes, and so on.

For forward compatibility, the user-defined class is the classic class by default, and the new class needs to inherit from the base class of all classes object or a new class that inherits from Object.

It is important to note that while using the latest Python (2.7), some features do not work in legacy classes.

Therefore, in order to ensure that you are using a new class, there are two ways to do this:

1. Meta-class, add the following code at the front of the class module code __metaclass__ = ClassName (a custom new Class).

2. Classes are inherited directly or indirectly from the built-in class object.

If you do not need to be compatible with legacy classes, the old version of the class is maintained as a new class.

In Python3, these problems do not exist, because all classes are subclasses of the object class (implicit).

code example:

Class Oldclass:            #经典类    def __init__ (self):        Pass  class Newclass (object):    #新类    def __init__ ( Self):        pass   c1 = Oldclass () C2 = Newclass ()  c1.__class__            # output 
 
  
   
  , type (c1)                # Output 
  
   
    
     , c2.__class__            # output
   
    
     
    , type (c2)                # output
    
      ,
   
    
  
   
 
  
  • Related Article

    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.