weird exceptions:(

Thread Solved
Reply

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

weird exceptions :(

 
0
  #1
Aug 30th, 2005
Hello, I'm trying to execute this simple code:

  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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: weird exceptions:(

 
0
  #2
Sep 1st, 2005
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:
  1. del drugi
  2. del prvi
If so, please include them, because the other code worked fine for me.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: weird exceptions:(

 
0
  #3
Sep 1st, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC