析构函数:__del__() 释放对象是自动调用
class Person(object):
def run(self):
print("run")
def eat(self, food):
print("eat" + food)
def __init__(self,name,age,height,weight):
self.name=name
self.age=age
self.height=height
self.weight=weight
def __del__(self):#当程序结束时运行
print("析构函数")
per1=Person("lili",20,175,50)
del per1 #手动释放对象
print(per1.name)#释放对象后程序不运行
#在函数里定义的对象,会在函数结束时自动释放,这样可以减少内存空间的浪费
def func():
per2=Person("x",2,45,7)
func()
转载于:https://www.cnblogs.com/zlong123/p/10471202.html