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 puzzling me.
Basically, if I run the following script (in Python 2.6.2)
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
def __del__(self):
'''I am dying'''
print '%s says bye' % self.name
Person4.population -= 1
if Person4.population == 0:
print 'I am the last one'
else:
print 'There are still %d left' % Person4.population
swaroop = Person4('Swaroop')
kaleem = Person4('Kalem')
using the Python console (or the Spyder interactive console) I see the following:
execfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Initializing Kalemexecfile(u'C:\1_eric\Python\test1.py')
Initializing Swaroop
Swaroop says bye
I am the last one
Initializing Kalem
Kalem says bye
I am the last one
Why is the __del__
method being called immediately after the __init__
on the second run?
I am guessing that since the same instance names ('swaroop' and 'kaleem') are being used that it is releasing the original instance and garbage collecting it. But, this seems to be playing havoc with the current population count.
What is going on here?
What is a good way to avoid this sort of confusion?
Avoid the use of __del__
?
Check for existing instance names before reusing them?
...
Thanks, Eric
__del__
method being called at any particular time anyway. In the future the garbage collector could change so that__del__
gets called at different times. – Entrammel