Python(析构函数)
来源:互联网 发布:访客网络永远保存密码 编辑:程序博客网 时间:2024/06/03 22:59
在Python的对象中,也存在构造函数(__init__
)和析构析构函数(__del__
)。这里简单说一下Python的析构函数的机制。
在Python中定义下面一个类
class A: def __init__(self): print("initialization") def my_print(self): print("my_print") def __del__(self): print("delete")
运行时,通过下面这样的赋值:
>>>a1 = A()initialization>>>a2 = a1 >>>a3 = a1
在Python中,如果只对 a1 进行析构del a1
,那么对象并没有被析构掉,只有将对象和引用都析构,才会调用析构函数。
>>>del a1>>>del a2>>>del a3 //a1 a2 a3 顺序无要求delete
如果这里有:
>>>a1 = A()initialization>>>a2 = a1 >>>a3 = a1>>>a4 = a1.my_print()>>>>del a1>>>del a2>>>del a3 delete>>>a4>>>
这时候 a4 存在,但返回类型为 ‘NoneType’。
阅读全文
0 0
- Python(析构函数)
- Python 构造函数、 Python 析构函数、Python 垃圾回收机制
- Python 构造函数、 Python 析构函数、Python 垃圾回收机制
- python之构造/析构函数
- python的“构造函数”和“析构函数”
- python中的构造函数和析构函数
- python __init__构造函数 和 __del__析构函数
- 【Python】构造函数、析构函数与可变参数传递
- python之类的构造和析构函数
- python之类的构造和析构函数
- python函数
- python 函数
- python 函数
- Python 函数
- python 函数
- PYTHON函数
- python 函数
- Python函数
- python3.5安装lxml没有etree
- 集合框架(二) 迭代器Iterator
- [bzoj4300] 绝世好题 动态规划
- 训练日记8.16
- HTML5 Canvas笔记 -- 3
- Python(析构函数)
- TCP协议以及三次握手
- 搞定网页兼容性
- 电路调试小技巧总结
- STM8S103之AD采样
- 工作利器:15款思维导图工具推荐
- Tensorflow深度学习之十六:placeholder的注解和reshape函数
- 【C语言】【unix c】静态库和动态库的区别
- 3172: [Tjoi2013]单词