Home > Article > Backend Development > Detailed example of destruction and release of python class objects
[Related recommendations: Python3 video tutorial ]
_init__
The function is the constructor of the Python class. When creating a class object, this function will be automatically called; it can be used to set some initialization information of the object when creating the object. and settings. __del__
The function is the destructor of the Python class. When the life cycle of a class object ends and is destroyed, this function will be automatically called; it is mainly used to release some of the space occupied by the object. resources etc. are as follows, and the implementation code of a demo class is written.
>>> class demo(): ... def __init__(self): ... print("init class") ... print(self) ... def __del__(self): ... print("del class") ... print(self) ... >>>
When an object of this type is created, it will call the __init__
function and print out "init class";
When an object of this type is destroyed, it will call __del__
Function, print out "del class".
>>> a1 = demo() init class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> a2 = demo() init class <__main__.demo instance at 0x7f328f7c6d40> >>> >>> >>> >>> a1 = demo() init class <__main__.demo instance at 0x7f328f7c6d88> del class <__main__.demo instance at 0x7f328f7c6cb0> >>>
First use the variable a1 to refer to a demo class object. At this time, print out "init class" and the object address referenced by the a1 variable 0x7f328f7c6cb0
;
Use variables a2 refers to another demo class object. At this time, "init class" is printed out, as well as the object address referenced by the a2 variable 0x7f328f7c6d40
;
The class objects referenced by the a1 and a2 variables are Two different objects 0x7f328f7c6cb0
and 0x7f328f7c6d40
.
Finally create a demo class object and use the a1 variable to point to it again. At this time, a1 refers to the new class object, and the reference address is 0x7f328f7c6d88
; at the same time, due to the object referenced by a1 before0x7f328f7c6cb0
No one refers to it anymore, so the space of the old demo class object is released, and "del class 0x7f328f7c6cb0
" is printed.
>>> def create_demo(): ... inst = demo() ... >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> >>> >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>> >>> >>> >>> create_demo() init class <__main__.demo instance at 0x7f328f7c6cb0> del class <__main__.demo instance at 0x7f328f7c6cb0> >>>
Define a function create_demo. The function of this function is to create a demo class object and use the inst variable to reference the created class object.
Call the create_demo() function once, you can see that the demo object is created with the address 0x7f328f7c6cb0
; then the demo object is released immediately; because the object is only valid within the scope of the create_demo function , the function ends, and the demo object is recycled and released.
Call the create_demo() function again, the phenomenon is the same: the demo object is created with the address 0x7f328f7c6cb0
; then the demo object is released immediately.
>>> def return_demo(): ... return demo() ...
Define the function return_demo, which creates a class object internally and returns the created class object.
>>> True True >>> return_demo() init class <__main__.demo instance at 0x7fc511eb8cb0> <__main__.demo instance at 0x7fc511eb8cb0> >>> >>> True del class <__main__.demo instance at 0x7fc511eb8cb0> True >>> >>> return_demo() init class <__main__.demo instance at 0x7fc511eb8cb0> <__main__.demo instance at 0x7fc511eb8cb0> >>> >>> >>> >>> True del class <__main__.demo instance at 0x7fc511eb8cb0> True >>> >>>
You can see that when the function return_demo() is called for the first time, the printed content shows that an object is created at this time, and the object address is 0x7fc511eb8cb0
; the function return_demo is used internally The return
statement returns the created class object, so when the function returns, the object 0x7fc511eb8cb0
will not be released.
Next, execute a Python statement: True
, and see that the object 0x7fc511eb8cb0
is released. Because after the program executes the return_demo() function, it is found that the subsequent program does not reference the object returned by return_demo(), so Python will release the object space 0x7fc511eb8cb0
.
The second time you perform the same operation, you can see the same phenomenon.
>>> v1_demo = None >>> v2_demo = None >>> print(v1_demo) None >>> print(v2_demo) None >>> True True >>> >>> v1_demo = return_demo() init class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(v1_demo) <__main__.demo instance at 0x7fc511eb8d88> >>> >>> True True >>> >>> >>> v2_demo = return_demo() init class <__main__.demo instance at 0x7fc511eb8dd0> >>> >>> print(v2_demo) <__main__.demo instance at 0x7fc511eb8dd0> >>> True True >>> >>> >>> >>> >>> v1_demo = None del class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(v1_demo) None >>>
The phenomenon of this code segment is basically the same as that of the previous code segment.
As you can see, v1_demo and v2_demo refer to class objects, so the values of v1_demo and v2_demo are no longer None
.
Finally, we reset v1_demo to None
. At this time, the object 0x7fc511eb8d88
referenced by v1_demo is released.
>>> g_demo = None >>> print(g_demo) None >>> >>> def return_gdemo(): ... global g_demo ... g_demo = demo() ... >>> >>> print(g_demo) None >>> return_gdemo() init class <__main__.demo instance at 0x7fc511eb8d88> >>> >>> print(g_demo) <__main__.demo instance at 0x7fc511eb8d88> >>> >>> True True >>>
[Related recommendations: Python3 video tutorial]
The above is the detailed content of Detailed example of destruction and release of python class objects. For more information, please follow other related articles on the PHP Chinese website!