Home >Backend Development >Python Tutorial >__new__ and __init__, new-style classes and classic classes in python2

__new__ and __init__, new-style classes and classic classes in python2

高洛峰
高洛峰Original
2016-10-18 09:35:181114browse

In python2.x, classes that inherit from object are called new-style classes (such as class A(object)) and classes that do not inherit from object are called classic classes (such as class A())

The main differences between classic classes are the following points:

  1. New-style class objects can directly obtain their own types through the __class__ attribute: type

  2. The order of inheritance search has changed. When classic classes have multiple inheritance, the order of attribute search is: First go deep into the left side of the inheritance tree, then go back and start looking for the right side (i.e., depth-first search); the new class multi-inherited attribute search order: first search horizontally, and then move up

Example:

Classic class: the search order is ( D,B,A,C)

>>> class A: attr = 1
...
>>> class B(A): pass
...
>>> class C(A): attr = 2
...
>>> class D(B,C): pass
...
>>> x = D()
>>> x.attr
1

The new-style class inheritance search program is breadth-first

New-style classes: the search order is (D,B,C,A)

>>> class A(object): attr = 1
...
>>> class B(A): pass
...
>>> class C(A): attr = 2
...
>>> class D(B,C): pass
...
>>> x = D()
>>> x.attr
2

  3. New-style classes added With the __slots__ built-in attribute, the type of instance attributes can be locked to the range specified by __slots__.

 4. The new-style class adds the __getattribute__ method

 5. The new-style class has a built-in __new__ method, while the classic class does not have a __new__ method but only an __init__ method

Note: The default in Python 2.x is Classic classes, only those that explicitly inherit object are new-style classes. In Python 3. The definition method is to write a classic class and check it using the dir function in python2. There is no __new__ method under 2. The __new__ method will first call the __new__ method. The __new__ method accepts the class currently being instantiated as the first parameter (the type of this parameter is type, which plays an important role in interactive programming between c and python. , if you are interested, you can search for relevant information), the return value is the instance generated by this creation, which is the first parameter self in the well-known __init__ method. Then there will be a question, what about this instance. Got it?

Notice that those with __new__ method are descendants of the object class, so if we want to override the __new__ method ourselves (note that if we don’t override it, we use the __new__ method of the parent class when creating the instance) , if the parent class does not exist, continue tracing back) You can get this instance by calling the __new__ method class of object (this is actually basically the same as the default mechanism in python), such as:

class display(object):
    def __init__(self, *args, **kwargs):
        print("init")
    def __new__(cls, *args, **kwargs):
        print("new")
        print(type(cls))
        return object.__new__(cls, *args, **kwargs)   
a=display()

Running the above code will The following output is obtained:

new

init

So we can get the following conclusion:

In the instance creation process, the __new__ method is called before the __init__ method, and its first parameter type is type.

If no other special processing is required, you can use the __new__ method of object to get the created instance (that is, self)

So we can find that we can actually use the __new__ method of other classes. Get this instance, as long as that class or its parent class or ancestor has __new__ method. In such a relationship, __init__ provides the raw material self for production (but it does not guarantee that the source of this raw material is authentic. Like the above, it uses the __new__ method class of another unrelated class to obtain this instance), and __init__ Use the ingredients given by __new__ to complete this object (although it does not know whether these ingredients are authentic)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn