Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Exception error when accessing the class variable at the terminationof the program

Thread Tools

Exception error when accessing the class variable at the terminationof the program
Posts: n/a
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): = name
print name, ' is now created'
Person.Count += 1

def __del__(self):
print, ' is now deleted'
Person.Count -= 1
if Person.Count == 0:
print 'The last object of Person class is now deleted'
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?


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Simple Newbie question about accessing a Variable out of a class of a class Christian Maier C++ 3 02-15-2007 08:24 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Accessing class variable at class creation time Carlos Python 7 09-26-2005 07:14 PM