首页 > 编程语言 > 详细

Python中类的使用(3析构函数__del__()使用)

时间:2020-05-26 18:33:45      阅读:58      评论:0      收藏:0      [点我收藏+]
析构函数:__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()

 

Python中类的使用(3析构函数__del__()使用)

原文:https://www.cnblogs.com/insane-Mr-Li/p/12967142.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!