This is the 28th day of my participation in the August Challenge
What are Python magic methods
Magic methods are methods surrounded by double underscores in Python classes, such as the common __init__, __new__, __del__, etc. These methods are automatically called when a class or object performs a specific operation, and we can use or rewrite these magic methods to add special functionality to our custom classes to suit our needs.
Common magic methods
init
The most common and familiar magic method is the __init__() method, which can be used to define an object’s initialization operation. Such as:
class MyClass:
def __init__(self, a, b) :
self.a = a
self.b = b
Copy the code
new
The __init__() method is easily thought of as the first method called when an object is instantiated, but it is not. When we instantiate an object, as in class1=MyClass(1, 2), the __new__() method is called first, followed by the __init__() method, The __new__() method actually creates the instantiated object, and the __init__() method simply initializes the object.
class MyClass:
def __init__(self, a, b) :
print("The __init__() method is called")
self.a = a
self.b = b
def __new__(cls, *args, **kwargs) :
print("The __new__() method is called")
print("The args.", args)
c = super().__new__(cls)
return c
class1 = MyClass(1.2)
Copy the code
The result is as follows:
Note that:The first argument to the __new__() method must be a CLS argument, representing the current class, and must return an instantiated object. There are no special requirements, so it is best not to override this method.
del
Called when an object is destroyed at the end of its life cycle, the __del__ method is called, not often, and generally does not need to be overridden.
str & repr
The __str__() method defines the behavior when called by print() or STR () methods, and the __repr__() method defines the behavior when called by print() or repr() methods. In general, there are no special differences in how they implement class-to-string conversions. In fact, the __str__() method should be more readable, and the __repr__() method should be more accurate. That is, the __str__() method is intended to be readable, while the __repr__() method is intended to be easy for developers to debug.
class MyClass:
def __str__(self) :
return "Instance of MyClass"
def __repr__(self) :
return self.__class__.__name__
class1 = MyClass()
print(str(class1))
print(repr(class1))
print(class1)
Copy the code
Note: When print() is executed, the __str__() method is executed first, and the __repr__() method is executed only if no __str__() method is defined.
call
The __call__() method lets instances of a class be called like functions. Such as:
class Multiply:
def __init__(self, x, y) :
self.x = x
self.y = y
def __call__(self, *args, **kwargs) :
return self.x * self.y
p = Multiply(2.3)
print(p())
Copy the code
When p() is called, the __call__() method is called, returning the product of arguments x and y, and the __call__() method can also accept arguments.
iter
If we want an instantiation object to be an iterator, it can be used for… In loops, we need to define the __iter__() and __next__() methods in the class. __iter__() returns an iterator, and __next__() returns the next element of the iterator, as discussed in the previous article: The implementation of the Iterator, which is not expanded here.
conclusion
Actually magic methods is far more than these, there are many, many are worth us to use, in Python magic methods just like its name has a magic, can always be convenient for you provide you need all kinds of demand, they are a big Python object-oriented programming tool, use good will have unexpected results, slowly to understand and use them.
Finally, thank my girlfriend for her tolerance, understanding and support in work and life!