


[Related recommendations: Python3 video tutorial ]
1. Class constructors and destructors
-
_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.
2. Code demonstration
1. Reference changes
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.
2. Class objects only inside the function
>>> 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.
3. The class object returned inside the function
>>> 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.
1. Use global variables to reference class objects inside functions
>>> 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!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
