I love the language of Python. After reading the grammar, I learned the Django Web development framework. Kind of familiar with Python. But there are a lot of things still do not know, because the use of less. Today I learned two magic methods: __new__ and __init__.
Open attack:
If you have a simple understanding of Python, you should know that it contains the concept of class. The syntax is as follows:
Copy Code code as follows:
Class ClassName:
<statement-1>:
.
.
.
<statement-N>
Here's the problem. As we have learned in C # or Java these languages, when declaring a class, there are constructors. This is similar to the following:
Copy Code code as follows:
public class classname{
Public ClassName () {
}
}
Of course, the access modifier doesn't have to be public, and that's not the point. What about the constructors in Python? My own understanding is that it is not a constructor. Only some internal, modified methods are invoked when initializing. For example: __new__ and __init__. The literal meaning of the __new__ should be performed before the __init__, and indeed after the data has been actually checked. The official document about the __init__ method has this sentence:
Copy Code code as follows:
Many classes like the Create objects with the instances customized to a specific the state. Therefore a class may define a special method named __init__ ()
This means that when you create a class, if you want to specify its initial state, you can do so by defining a method that specifies the name as __init__. So __new__ is not the official recommended method for initializing classes. But __new__ was executed before __init__. The first sentence introduced to __init__ in the official document is that when the instance is created, the __init__ method is called (called when the instance is created.), which is described later, if the __init__ method that wants to call the base class must be explicitly invoked, Inheriting only the base class does not automatically invoke the __init__ method of the base class when the subclass is initialized. This should be regarded as a __init__ approach to understand.
Now let's take a look at the __new__ method. First look at the official documents:
Copy Code code as follows:
Called to create a new instance of the class CLS. __new__ () is a static method (special-cased So, need not declare it as such) which takes the class of which an instance is requested as its a-argument. The remaining arguments are those passed to the object constructor expression (the call to the Class). The return value of __new__ () should is the new object instance (usually an instance of the CLS).
Typical implementations create a new instance of the class by invoking the superclass ' s __new__ () method using Super (Curre Ntclass, CLS). __new__ (cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary Fore returning it.
If __new__ () returns an instance is the CLS, then the new instance ' s __init__ () method would 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 a instance of the CLS, then the new instance ' s __init__ () method would be invoked.
__NEW__ () is intended mainly to allow subclasses the immutable types (like int, str, or tuple) to customize instance On. It is also commonly the overridden in custom metaclasses the to customize class creation.
As you can see from here, this method is where the instances of the class are generated. When the instance is created, the __init__ method is called, initializing the internal state value of the class. The document also mentions that the __new__ method is actually a static method that is not declared every time a class is defined, because after version 2.4 object is the base class for all objects, and __new__ is the static method that is defined inside object objects.
It's almost the same here, just by literal understanding. __NEW__ is used to create an object, and __init__ initializes the object state after the creation completes. The first two days saw an interesting method by using __new__ to apply a single case pattern to the object.
Note: In class inheritance, when subclasses and parent classes define their own __new__ methods, the __new__ method of the subclass is invoked before the parent class is invoked. This is the same as the inheritance in C #. In fact, thinking about Python is simply to separate the two concepts of initializing and creating objects, and that's not the way in C #, which only gives constructors that developers can look at. From this point of view, I think Python's approach I prefer.
End:
Today, I learned a new knowledge, I am very happy. Practice Practice again ...