Take notes on how to understand the magic methods of _ new _ and _ init _ in Python, __new ___ init _

Source: Internet
Author: User

Take notes on how to understand the magic methods of _ new _ and _ init _ in Python, __new ___ init _

I like Python very much. After reading the syntax, I learned about the Django Web development framework. I am familiar with Python. However, I still don't know a lot about it, because it is rarely used. Today I learned two magic methods: __new _ and _ init __.

Attack:

If you have a brief understanding of Python, you should know that it includes the concept of class. Syntax:

Copy codeThe Code is as follows:
Class ClassName:
<Statement-1>:
.
.
.
<Statement-N>

The problem arises. For example, in the C # or Java language we learned, class declaration involves constructors. Similar to the following:

Copy codeThe Code is as follows:
Public class ClassName {
Public ClassName (){

}
}

Of course, the access modifier does not have to be public. If this is not the key, it will not be too long. What is the constructor in Python? In my own opinion, there is no constructor. However, some internal changeable methods will be called during initialization. For example, __new _ and _ init __. To understand the literal meaning, _ new _ should be executed before _ init _. This is true after the materials are checked. The _ init _ method in the official document has the following sentence:

Copy codeThe Code is as follows:
Classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named _ init __()

This means that when creating a class, if you want to specify its initial state, you can define a method named _ init _ to implement this function. In this case, _ new _ is not the method officially recommended for class initialization. But _ new _ is executed before _ init. The first sentence about _ init _ in the official document is: Call the _ init _ method (Called when the instance is created.) when creating an instance .), later, we will introduce that if you want to call the _ init _ method of the base class, you must call it explicitly, the _ init _ method of the base class is not automatically called when the base class is initialized. So far, we should understand the _ init _ method.

Next, let's take a look at how the _ new _ method works. Take a look at the official documentation:

Copy codeThe Code is as follows:
Called to create a new instance of class cls. _ new _ () is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. the remaining arguments are those passed to the object constructor expression (the call to the class ). the return value of _ new _ () shocould be the new object instance (usually an instance of cls ).

Typical implementations create a new instance of the class by invoking the superclass's _ new _ () method using super (currentclass, cls ). _ new _ (cls [,...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If _ new _ () returns an instance of cls, then the new instance's _ init __() method will be invoked like _ init _ (self [,...]), where self is the new instance and the remaining arguments are the same as were passed to _ new __().

If _ new _ () does not return an instance of cls, then the new instance's _ init _ () method will not be invoked.

_ New _ () is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. it is also commonly overridden in custom metaclasses in order to customize class creation.

It can be seen from this that this method is the place where the class instance is generated. After the instance is created, call the _ init _ method to initialize the internal status value of the class. The document also mentions that the _ new _ method is actually a static method. You do not need to declare this method every time you define a class, because after version 2.4, the object is the base class of all objects, _ new _ is a static method defined inside the object.

It's almost the same here, so you can simply understand it. _ New _ is used to create an object, and _ init _ is used to initialize the object state after the object is created. Two days ago, I saw an interesting method. I applied the singleton mode to the object by using _ new.

Note: In class inheritance, when both the subclass and parent Classes define their own _ new _ methods, then the _ new _ method of the subclass is called before the parent class is called. This is the same as inheritance in C. In fact, in Python, we just separated the concepts of initialization and object creation. in C #, we didn't do this. We only gave the constructor, and developers could do it themselves. From this point on, I think I prefer Python.

End:

Today, I learned new things and I am very happy. Practice again...

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.