Python object deleting itself

后端 未结 14 2003
一向
一向 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:41

    what you could do is take the name with you in the class and make a dictionairy:

    class A:
      def __init__(self, name):
        self.name=name
      def kill(self)
        del dict[self.name]
    
    dict={}
    dict["a"]=A("a")
    dict["a"].kill()
    

提交回复
热议问题