Exception error when accessing the class variable at the terminationof the program

J

jwalana

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top