Exception error when accessing the class variable at the termination of the program

jwalana at vsnl.net jwalana at vsnl.net
Mon Feb 2 02:52:51 EST 2009


Hi All,

   Here is a sample piece of code with which I am having a problem, with Python version 2.4.4

class Person:
    Count = 0   # This represents the count of objects of this class

    def __init__(self, name):
        self.name = name
        print name, ' is now created'
        Person.Count += 1

    def __del__(self):
        print self.name, ' is now deleted'
        Person.Count -= 1
        if Person.Count == 0:
            print 'The last object of Person class is now deleted'
        else:
            print 'There are still', Person.Count, ' objects of class Person'

x2 = Person("Krishna")
del x2

  When the above code is executed, it works properly.

  If the last statement del x2, is removed, then when the program terminates, this throws up an exception as shown below

Krishna  is now created
Krishna  is now deleted
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'Count'
" in <bound method Person.__del__ of <__main__.Person instance at 0xf6532f6c>> ignored

 Can someone please explain why the exception happens in the case where there is no explicit del statement?

Thanks
Jana




More information about the Python-list mailing list