python类 析构方法

对象被销毁时执行的操作。一般用于资源回收。Python有垃圾回收机制会自动调用__del__,也可自己调用。

# -*- coding: UTF-8 -*-
class Student:
    company = 'LOL'
    def __del__(self):
    print("销毁对象{0}".format(self))

s1 = Student()
s2 = student()
del s1

"销毁对象"s1和s2都会被调用,s2有回收机制调用。

原文地址:https://www.cnblogs.com/zzm-blog/p/10723553.html