In Python, all methods that are double-underwritten with "_ _" are called "magic methods"
Magic method Python interpreter automatically gives the default, so unless you need to change its internal function, other times use the default magic method
The three most commonly used: "init", "_new", "del_"
_* new is used to create a class and return an instance of this class. ** _init * will pass in parameters to initialize the instance and initialize the sample properties, forming a "constructor" together with new
* del* destroy the instantiated object, which is the destructor
Class call: call
* call* allows a class to be called like a function
Attribute access: getattr, setattr, delattr
_* This method is called when getattr accesses the non-existent attributes of the object to define the access behavior ** _setattr* is called when the object attributes are set
* delattr* called when deleting object attributes
Context manager: _enter and _exit
For these two methods, please see question 3 above.
Iterator methods: iter and \__next__
* iter: returns a container iterator, in many cases it will return an iterator, especially when the built-in iter() method is called, and when using for x in container: loop. Iterators are their own objects, and they must define an iter method that returns self.
_ next_*: returns the next element of the iterator
Recommended Posts