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

Discussion in 'Python' started by jwalana@vsnl.net, Feb 2, 2009.

  1. Guest

    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
    , Feb 2, 2009
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,699
    Thomas Weidenfeller
    Oct 12, 2005
  2. Carlos
    Replies:
    7
    Views:
    279
    Dave Hansen
    Sep 26, 2005
  3. Mohanasundaram
    Replies:
    44
    Views:
    1,036
    Keith Thompson
    Aug 24, 2004
  4. Christian Maier
    Replies:
    3
    Views:
    489
    John Harrison
    Feb 15, 2007
  5. mahesh
    Replies:
    0
    Views:
    1,417
    mahesh
    Mar 8, 2007
Loading...

Share This Page