The method of the double python --- bring you to see the underlying logic python (__ str __, __ repr __, __ del __, __ call__)

We are in the process of writing code, often pick up directly call functions such as len (), print () input () and so on, in fact, these methods or functions during the call, triggered a lot of internal operating mechanism and the underlying logic, after in-depth research, we can see a different python, the following share a few two-under method and their operating logic with you.
Double Down method: refers to the method with double-underlined, such as our very common in the class __init__, today we want to introduce __str__, repr , del , Call :
---------- -------------------------Dividing line----------------------- ------------
str
we define such a class, and instantiate:

class A:
    pass
a=A()
print(a)

The output is:
< main II.A, AT 0x0000026034D36E08 object>
This result is actually kind of memory addresses, and can see that he is a subclass of object, modify the code test again:

class A:
    pass
    def __str__(self):
        return '我已经打印了'
a=A()
print(str(a))
print(a)

At this output is:
I have already printed
I have printed
__str__ method explained in the class is called twice, which is the logic:
When we call str () method in the class, or print an object triggered __str__ method, we can use this feature to set the return value is needed as a reference; when the object is a printing program __str__ default method calls to a target, and when the object is not a set, when you find him to call the parent class object, which is why we set up __str__ method, the reason will be triggered.
-----------------------------------Dividing line------------- ----------------------
the repr
the repr () function to convert the object for read by the interpreter, and simply is converted into its original form. such as

a='1'
print(a)
print(repr(a))

Print Results:
1
'1'
though a string type of numbers, but when we print directly shows the number 1, when using the repr, a string is displayed, that is, his original form. Or the same example above, we will change repr str

class A:
    def __repr__(self):
        return '我已经打印了repr'
a = A()
print(a)

The results are as follows:
I have printed repr

Similarly, when a print object class, this will trigger methods, results and __str__ look the same; if there __str__ and __repr__ the same time, the order is how the code under test?

class A:
     def __str__(self):
        return '我已经打印了str'
     def __repr__(self):
        return '我已经打印了repr'
a = A()
print(a)

The results are as follows:
I have printed str

Run logic: when str__ and __repr__ exist, precedence __str , when there is no __str__, will continue __repr__.
-----------------------------------Dividing line------------- ----------------------
del__ destructor method: when we program execution is completed, it will release the memory, then it will trigger __del . Such as the following examples:

class A:
    def __del__(self):
        print( '我已经执行了')
a = A()
del a

We instantiate the class A object a, and then delete a, it will trigger __del__ method.
The results are as follows:
I have performed
at this time if we print the object a, will find the following results:
NameError: name 'A' IS not defined
description we use del to delete, it will not only trigger __del__ method will also delete objects and it is the first implementation of the former.
Because he not only in the use of del trigger will be triggered at the end of the program, memory release, we can use __del__ as finishing work, so operations such as closing files.
-----------------------------------Dividing line------------- ----------------------
Call : this method usually we rarely see, but very interesting, look at the following code

class A:
    def __call__(self):
        print('我已经执行了')
a = A()()

The results:
I have already implemented

From the above results can be inferred, when an object plus (), the trigger __call__ method, actual operating results and our guess is the same.
Summary:
. 1) STR : triggered when printing in the class
2) the repr : triggered when printing in the class, but the presence of __str__, preferentially executed __str__
. 3) del : del triggered when performing or releasing memory
. 4) Call : fires when the object is added ()
more applications, you can try to explore

Published 13 original articles · won praise 1 · views 189

Guess you like

Origin blog.csdn.net/aa12551827/article/details/104919173