__new__ and __init__ in Python2, new class and classic class

Source: Internet
Author: User
In python2.x, classes that inherit from object are called modern classes (such as Class A (object)) that do not inherit from object are called Classic classes (such as Class A ()).

The difference between the new class and the classic class is mainly the following points:

1. The new class object can get its own type directly through the __class__ property: type

2. The order of inheritance search has changed, the classic class multi-Inheritance time attribute search order: The first deep inheritance tree left, and then return, start looking for the right side (that is, depth-first search); New class multi-inheritance Property search Order: Horizontal Search first, then move up

Example:

Classic class: Search order Yes (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.attr1

New class Inheritance Search program is width first

New class: Search Order Yes (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.attr2

3. The new class adds __slots__ built-in properties that can lock the type of instance attribute to the scope specified by the __slots__.

4. The new class adds the __getattribute__ method

5. The new class has a built-in __new__ method and the classic class has no __new__ method and only the __init__ method

Note: Python 2.x is a classic class by default, and only object that is explicitly inherited is a new class

Python 3.x, by default, is a new class (that is, the object class is the ancestor of all classes by default) and does not have to inherit objects explicitly (you can write a classic class as defined in the classic class and use the Dir function test in python2.x and 3.x versions, respectively).

For example: Class A ():

Pass

Print (dir (A))

It is found that there is no __new__ method at 2.x and 3.x.

Next, the difference between __new__ method and __init__:

When you create an instance of a class in Python, if the class has the __new__ method, the __new__ method is called first, and the __new__ method accepts the class that is currently being instantiated as the first parameter (the type of this parameter is This type plays an important role in the interactive programming of C and Python, where the relevant data can be searched, and the return value is the instance created by this creation, which is the first parameter self in the __init__ method we are familiar with. Then there will be a question, how does this example get?

Notice that the __new__ method is a descendant of the object class, so if we ourselves want to rewrite the __new__ method (note that the __new__ method of the parent class is used when creating the instance without overwriting, if the parent class does not go upstream), you can call the object's __new The method class gets this instance (which in fact is 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 give you the following output:

New

Init

So we can get the following conclusion:

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

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

We can then find that this instance can actually be obtained using the __new__ method class of other classes, as long as the class or its parent class or ancestor has a __new__ method.

Class another (object):    def __new__ (Cls,*args,**kwargs):        print ("Newano")        return object.__new__ (CLS, * args, **kwargs)   class Display (object):    def __init__ (self, *args, **kwargs):        print ("Init")    def __new_ _ (CLS, *args, **kwargs):        print ("Newdis")        print (Type (CLS))        return another.__new__ (CLS, *args, **kwargs)   A=display ()

The above output is:

Newdis
 
  
   
  newanoinit
 
  

All we find __new__ and __init__ like such a relationship, __INIT__ provides the raw material for production self (but does not guarantee that this raw material source is authentic, like above it uses another unrelated class of __new__ method class to get this instance), and __init__ Use __new__ to refine the object (though it doesn't know if the ingredients are authentic)

  • 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.