__del__ method being called in python when it is not expected

前端 未结 2 1203
予麋鹿
予麋鹿 2020-12-11 05:16

I am new to python and have been working through the examples in Swaroop CH\'s \"A Byte of Python\". I am seeing some behavior with the __del__ method that is

2条回答
  •  悲&欢浪女
    2020-12-11 06:01

    General advice: don't use __ del __ in Python. It can break garbage collection in a number of ways, esp. in the case of circular references between objects.

    In your example, there're various issues related to the usage of execfile() - which is not a best practice - and the redefinition of global variables. By the way, if you really need to create a pseudo-destructor (i.e. a code that is invoked whenever the object gets garbage collected), write a so-called "finalizer" function (it's not properly a destructor) and invoke it using weakref.ref callback. It should NOT be an instance method of course, and remember that lambda actually creates a closure, hence be sure not to leak any reference to self in the callback! If you need data from the destroyed instance, use the func default argument approach, just be sure never to reference 'self' inside the lambda, otherwise it won't work.

    from weakref import ref
    from time import sleep
    
    class Person4:
        '''Represents a person'''
        population = 0
    
        def __init__(self, name):
            '''Initialize the person's data'''
            self.name = name
            print 'Initializing %s'% self.name
    
            #When the person is created they increase the population
            Person4.population += 1
    
            self._wr = ref(self, lambda wr, name=self.name: Person4_finalizer(name))
    
    def Person4_finalizer(name):
            '''I am dying'''
            print '%s says bye' % name
    
            Person4.population -= 1
    
            if Person4.population == 0:
                print 'I am the last one'
            else:
                print 'There are still %d left' % Person4.population
    
    p1 = Person4("one")
    p2 = Person4("two")
    p3 = Person4("three")
    
    del p2
    del p3
    sleep(5)
    

    output (the sleep is there to help see what's happening):

    Initializing one
    Initializing two
    Initializing three
    two says bye
    There are still 2 left
    three says bye
    There are still 1 left
    one says bye
    I am the last one
    

提交回复
热议问题