944,110 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2207
  • Python RSS
Aug 30th, 2005
0

weird exceptions :(

Expand Post »
Hello, I'm trying to execute this simple code:

Python Syntax (Toggle Plain Text)
  1. class Person:
  2. population = 0
  3. def __init__(self, name):
  4. print 'Creating % s peron' % name
  5. self.name = name
  6. Person.population += 1
  7. def __del__(self):
  8. print 'Destroying %s person' % self.name
  9. Person.population -= 1
  10.  
  11.  
  12. prvi = Person('Asimi')
  13. drugi = Person('Mici')

I'm using Wing IDE trial and here's what is output in debug mode:

Creating Asimi peron
Creating Mici peron
Destroying Mici person
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0x00B29558>> ignored
Destroying Asimi person
Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0x00B29530>> ignored

Problem is solved by putting self.population - = 1 in __del__ method, but the example is from book ByteOfPython and population is supposed to be class variable...

I really don't have a clue what is wrong here...
Similar Threads
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Sep 1st, 2005
0

Re: weird exceptions:(

It looks like you haven't included all the code you wrote to test this. Were there statements after 'drugi = Person('Mici')'? Statements like, perhaps:
Python Syntax (Toggle Plain Text)
  1. del drugi
  2. del prvi
If so, please include them, because the other code worked fine for me.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Sep 1st, 2005
0

Re: weird exceptions:(

Look at variable k in the code snippet at:
http://www.daniweb.com/code/snippet354.html
This is a similar situation, it is declared outside of the class methods and needs self to bring it into the methods. The class name won't do. If your book uses this, then it is wrong (can happen). Maybe very early versions of Python allowed this.

Remember, special method __init__() is called first and acts as constructor. Special method __del__() acts as the mostly optional destructor.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Plotting math functions (Python)
Next Thread in Python Forum Timeline: nice online book with examples





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC