Python object deleting itself

后端 未结 14 2010
一向
一向 2020-11-27 04:23

Why won\'t this work? I\'m trying to make an instance of a class delete itself.

>>> class A():
    def kill(self):
        del self


>>>          


        
14条回答
  •  囚心锁ツ
    2020-11-27 04:55

    Indeed, Python does garbage collection through reference counting. As soon as the last reference to an object falls out of scope, it is deleted. In your example:

    a = A()
    a.kill()
    

    I don't believe there's any way for variable 'a' to implicitly set itself to None.

提交回复
热议问题