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有回收机制调用。

 

posted @ 2019-04-17 14:56  文二1234  阅读(2963)  评论(0编辑  收藏  举报