"Python" Python New class Introduction

Source: Internet
Author: User

This article was reprinted from: Kaka_ace ' s blog

When we develop with Python, we encounter the notation of Class A and Class A (object),
This is a conceptual and functional distinction in Python2, the distinction between classic classes (old Class) and modern classes,
Described in English as Old-style (Classic-style) and New-style respectively.

Through the search, we first looked at three data links:
Official documents
StackOverflow Solutions
Python Types and Objects

According to StackOverflow the answer leads to the language inventor Guido wrote an article:
The Inside story on New-style Classes
The summary can be interpreted as an official explanation:

    • Low-level constructors named __new__ ()-low-level constructors.
      The class __init__ of Note:python is not a constructor in other linguistic sense,
      The function that initializes the instance properties after new creates the instance.

    • Descriptors, a generalized-customize attribute access– descriptor.
      or descriptor protocol support. Descriptor protocol __get__, __set__, __delete__ et,
      Can read descriptor documentation

    • Static methods and class methods-statically methods and classes methods

    • Properties (computed attributes) – property accesses setter getter.

    • Decorators (introduced in Python 2.4) – Adorner.
      Now decorator syntax sugar is spread across the python framework.

    • slots– the properties of an instance after the user has been set.
      Replacing __dict__ in Python2 can save nearly 2/3 of memory, Python3
      Do not use slots because of optimized memory usage, because __DICT__ structure memory is optimized,
      Note: __dict__ is not a Python built-in dict, it is actually a proxy class.

    • A new method Resolution order (MRO) –mro Methods resolution order Change
      (change from left recursion to C3 algorithm)

Another answer adds a bit:

    • Super Added–super Method Support

For C3MRO Introduction, you can click on the link

The following is a translation of the official documents (Python2 content)

Classes and instances come in the Flavors:old-style (or classic) and New-style.
Classes and instances are available in two ways: Legacy (or Classic) and modern classes.

Up to Python 2.1 The concept of class is unrelated to the concept of type,
And Old-style classes were the only flavor available. For an Old-style class,
The statement x.__class__ provides the class of x, but type (x) are always
<type ' instance '>. This reflects, the fact and all Old-style instances, Independent
Of their class, is implemented with a single built-in type, called instance.

All the way to Python the concept of 2.1 class is not the same as type, and the old class is the only style. For a
In the old class, the declarative sentence x.__class__ provides the class of x, but type (x) has always been <type ' instance '>.
It reflects all the old class instances, independent of their class, which are implemented by a simple built-in type,
is called instance instance.

New-style classes were introduced in Python 2.2 to unify the concepts of class
and type. A New-style class is simply a user-defined type, no more, no less.
If X is an instance of a New-style class, then type (x) is typically the same as
X.__class__ (although this isn't Guaranteed–a New-style class instance is
Permitted to override the value returned for x.__class__).

The new class was introduced in Python 2.2 to unify the concepts of class and type. A new class simplifies the user
Custom type, not many more. If X is an instance of a modern class instance, type (x) typically
and x.__class__ (although this is not guaranteed – a new class is allowed to rewrite the value of x.__class__) consistently.

eg
>>> class A (object): Pass
>>> a = A ()
>>> a.__class__
>>> <class ' __main__. A '>
>>>
>>> Type (a)
>>> <class ' __main__. A '>

The type and __class__ of Class A are <type ' type '>

>>> A.__class__
>>> <type ' type '>

The major motivation for introducing New-style classes are to provide a unified
Object model with a full meta-model. It also has a number of practical benefits,
Like the ability-subclass most built-in types, or the introduction of
"Descriptors", which enable computed properties.

The main improvement in introducing a new class is to provide a unified object model with a complete meta-model.
It can have a lot of use value, similar to the ability or descriptor of the subclass of the built-in type
Descriptors Introduction (allowable computed properties)

For compatibility reasons, classes is still old-style by default. New-style
Classes is created by specifying another New-style class (i.e. a type) as a
The parent class, or the "top-level type" object if no other parent is needed. The behaviour of New-style classes differs from, the Old-style classes in
A number of important details in addition to what type () returns. Some of these
Changes is fundamental to the new object model, like the The special methods
is invoked. Others is "fixes" that could is not being implemented before for
Compatibility concerns, like the method resolution order in case of multiple
Inheritance.

With the compatibility factor in mind, class use defaults to the Legacy class. The new class is specified by another new class
(such as a type) to be created as a parent class, or as a top-level class object (if no other parent class is required).
In addition to the return mode of type (), the behavior of the new class and the old class is distinguished from a large number of important details. These changes
For the new class Oject model is fundamental, similar to the way a particular method is called. Other
Attributes are fixes for compatibility issues that were previously not possible, such as the order of the MRO method resolution under multiple inheritance.

Tips:
1. Legacy class multiple inheritance uses the left recursive traversal method
2. The new class multiple inheritance algorithm uses the C3 algorithm.

While the manual aims to provide comprehensive coverage of Python ' s class
Mechanics, it may still is lacking in some areas when it comes to its coverage
of New-style classes. Please see Https://www.python.org/doc/newstyle/for
Sources of additional information.

The goal of the handbook is to provide a comprehensive coverage of Python's class mechanism, covering the new class
, it may still lack information in some areas. Please read https://www.python.org/doc/newstyle/
To receive additional sources of information.

Old-style classes is removed in Python 3, leaving only New-style classes.
Legacy classes are removed in Python3, preserving only the new class.

original articles, reproduced please specify: reproduced from Kaka_ace's blog

Resources:

1.http://www.zhihu.com/question/19754936

"Python" Python New class Introduction

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.