__init__ 与 __del__

来源:互联网 发布:淘宝全球购要身份证 编辑:程序博客网 时间:2024/06/03 22:58


__init__方法在类的一个对象被建立时,马上运行。


class Person:    def __init__(self, name):        self.name = name    def sayHi(self):        print 'Hello, my name is', self.namep = Person('Swaroop')p.sayHi()


__del__,它在对象消逝的时候被调用。对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。

当对象不再被使用时,__del__方法运行,但是很难保证这个方法究竟在 什么时候 运行。如果你想要指明它的运行,你就得使用del语句

原创粉丝点击