Python modern class and Legacy class

Source: Internet
Author: User

New and Legacy classes

The new Python class is introduced in version 2.2, and we can call the previous Class A classic or old 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 <type ' instance ' >. 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 the following methods:

    1. Put this assignment statement at the front of the class module code __metaclass__ = Type (mentioned earlier).

    2. Their classes inherit 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.
Of course, there are no such problems in Python3, because all classes are subclasses of the object class (implicit).


code example:

class cc:             #经典类      def __init__ ( self ): &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PASSCLASS&NBSP;CCN ( Object):     #新类     def __init__ ( self ):   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PASS&NBSP;C1&NBSP;=&NBSP;CC () C2&NBSP;=&NBSP;CCN () c1.__class__             #  output-> <class __main__. Cc at 0x0137bf10>type (C1)                  #  output-> <type  ' instance ' >c2.__class__             #  output-><class  ' __main__. CCN ' >type (C2)                  #  output-><class  ' __main__. CCN ';d IR (C1) 

Python modern class and Legacy class

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.