析构函数:__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()

 

相关文章:

  • 2022-12-23
  • 2021-07-10
  • 2022-02-16
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
  • 2021-10-01
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2021-07-20
相关资源
相似解决方案