<br>I changed the last few lines to read:<br><br><br> 37 kalam.howMany()<br> 38 c = Person('Catherine', 'F')<br> 39 #cathy.sayHi()<br> 40 #cathy.howMany()<br> 41 #swaroop.sayHi()<br> 42 #swaroop.howMany()<br>
 43 <br><br><br>And I don't get the error.<br><br>However if I change line 38 to read<br>ca = Person('Catherine','F')<br><br>It again initializes and then the error occurs.  It isn't random, I can run the code 10, 20 times with variable name 'c' and no error, and then again with variable name 'ca' or 'cathy' and the error does occur.<br>
<br>I haven't much experience with __del__() functions but you're right -- this is strange as all hell.  And I can't even imagine why it would happen at all.<br><br>Obviously it is attempting to access the Person.population() variable.<br>
Which is a logic error, (I think) since well, we have 3 instances of the class being created and then the unbound variable (ie: a variable not associated with any instantiation of the class) being incremented and decremented.<br>
<br>Logically, I think it shouldn't work at all, but somehow this unbound variable is working but in a very buggy way.<br><br>Or am I wrong?  Are unbound class-variables supposed to be used to coordinate between multiple instantiations of that class?  <br>
<br><div><span class="gmail_quote">2008/3/23, George Sakkis <<a href="mailto:george.sakkis@gmail.com">george.sakkis@gmail.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Mar 23, 8:01 pm, QS <<a href="mailto:qingshan.c...@gmail.com">qingshan.c...@gmail.com</a>> wrote:<br> > Hi to all!<br> > I am new to python, and I encountered a weird problem.<br> ><br> > Here is my code<br>
 ><br> > ##########>8####################<br> > #!/usr/bin/python<br> > # Filename: objvar.py<br> > class Person:<br> >     '''Represents a person.'''<br> ><br> >     population = 0<br>
 >     #sex = 'F'<br> >     #age = 22<br> >     # It is vague here: is this variable going to be a class, or<br> > object, variable<br> ><br> >     def __init__(self, name, sex):<br> >         '''Initializes the person's data.'''<br>
 >         <a href="http://self.name">self.name</a> = name<br> >         self.sex = sex<br> >         print '(Initializing %s )' % <a href="http://self.name">self.name</a><br> >         # When this person is created, he/she<br>
 >         # adds to the population<br> >         Person.population += 1<br> ><br> >     def __del__(self):<br> >         '''I am dying.'''<br> ><br> >         print '%s says bye.' % <a href="http://self.name">self.name</a><br>
 >         Person.population -= 1<br> >         if Person.population == 0:<br> >             print 'I am the last one.'<br> >         else:<br> >             print 'There are still %d people left.' %<br>
 > Person.population<br> ><br> >     def sayHi(self):<br> >          '''Greeting by the person.<br> ><br> >          Really, that's all it does.'''<br> ><br> >          self.age = 25<br>
 >          print 'Hi, my name is %s, and I am %s, and I am age %d ' %<br> > (<a href="http://self.name">self.name</a>, self.sex, self.age)<br> ><br> >     def howMany(self):<br> >          '''Prints the current population.'''<br>
 >          if Person.population == 1:<br> >              print 'I am the only person here.'<br> >          else:<br> >              print 'We have %d persons here.' % Person.population<br> ><br>
 > swaroop = Person('Swaroop', 'M')<br> > swaroop.sayHi()<br> > swaroop.howMany()<br> > kalam = Person('Abdul Kalam', 'M')<br> > kalam.sayHi()<br> > kalam.howMany()<br> > cathy = Person('Catherine', 'F')<br>
 > cathy.sayHi()<br> > cathy.howMany()<br> > swaroop.sayHi()<br> > swaroop.howMany()<br> ><br> > ############# 8< #########################<br> ><br> > When I run this script, I got the following exception:<br>
 > Exception exceptions.AttributeError: "'NoneType' object has no<br> > attribute 'population'" in <bound method Person.__del__ of<br> > <__main__.Person instance at 0xb7d8ac6c>> ignored<br>
 ><br> > To to newcomer like me, this message doesn't make much sense. What<br> > seems weird to me is that, if I change the variable cathy to something<br> > else, like cath, or even cat, then the script will finish gracefully.<br>
 > Why "cathy" is not liked?!!<br> ><br> > Some of you may have recognized that the code is derived from a sample<br> > code in Swaroop's "A byte of python".<br> ><br> > My python is of version 2.5.1, on Ubuntu.<br>
 <br> <br>That's really weird... it's reproducible on Windows too. It doesn't<br> make any sense why the name of the variable would make a difference.<br> My guess is you hit some kind of obscure bug.<br> <br>
<br> George<br> <br>--<br> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br> </blockquote></div><br>