[Tutor] __del__ exception

Blaise Morose jdeambulemorose at gmail.com
Thu Jun 5 22:14:23 CEST 2008


Hi,

I have this silly piece of code that I am experimenting with:

#!/usr/bin/python

class Person:
        population = 0

        def __init__(self, name):
                self.name = name
                print '%s has been added' %self.name

                Person.population += 1

        def __del__(self):
                print '%s is leaving' % self.name

                Person.population -= 1

                print 'population = %d' %Person.population


p = Person('Jean')
d = Person('Michael')

Output:

Jean has been added
Michael has been added
Michael is leaving
population = 1
Jean is leaving
*Exception exceptions.AttributeError: "'NoneType' object has no attribute
'population'" in <bound method Person.__del__ of <__main__.Person instance
at 0xb7dadacc>> ignored*

So once all objects have been destroyed, an exception is triggered on class
variable 'population'.

Any more logical explanation and work around? I just need to understand what
python is doing here

Thx

Blaise
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080605/95aa6830/attachment.htm>


More information about the Tutor mailing list