Does python hate cathy?

Michael Wieher michael.wieher at gmail.com
Mon Mar 24 02:03:32 CET 2008


I changed the last few lines to read:


 37 kalam.howMany()
 38 c = Person('Catherine', 'F')
 39 #cathy.sayHi()
 40 #cathy.howMany()
 41 #swaroop.sayHi()
 42 #swaroop.howMany()
 43


And I don't get the error.

However if I change line 38 to read
ca = Person('Catherine','F')

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.

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.

Obviously it is attempting to access the Person.population() variable.
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.

Logically, I think it shouldn't work at all, but somehow this unbound
variable is working but in a very buggy way.

Or am I wrong?  Are unbound class-variables supposed to be used to
coordinate between multiple instantiations of that class?

2008/3/23, George Sakkis <george.sakkis at gmail.com>:
>
> On Mar 23, 8:01 pm, QS <qingshan.c... at gmail.com> wrote:
> > Hi to all!
> > I am new to python, and I encountered a weird problem.
> >
> > Here is my code
> >
> > ##########>8####################
> > #!/usr/bin/python
> > # Filename: objvar.py
> > class Person:
> >     '''Represents a person.'''
> >
> >     population = 0
> >     #sex = 'F'
> >     #age = 22
> >     # It is vague here: is this variable going to be a class, or
> > object, variable
> >
> >     def __init__(self, name, sex):
> >         '''Initializes the person's data.'''
> >         self.name = name
> >         self.sex = sex
> >         print '(Initializing %s )' % self.name
> >         # When this person is created, he/she
> >         # adds to the population
> >         Person.population += 1
> >
> >     def __del__(self):
> >         '''I am dying.'''
> >
> >         print '%s says bye.' % self.name
> >         Person.population -= 1
> >         if Person.population == 0:
> >             print 'I am the last one.'
> >         else:
> >             print 'There are still %d people left.' %
> > Person.population
> >
> >     def sayHi(self):
> >          '''Greeting by the person.
> >
> >          Really, that's all it does.'''
> >
> >          self.age = 25
> >          print 'Hi, my name is %s, and I am %s, and I am age %d ' %
> > (self.name, self.sex, self.age)
> >
> >     def howMany(self):
> >          '''Prints the current population.'''
> >          if Person.population == 1:
> >              print 'I am the only person here.'
> >          else:
> >              print 'We have %d persons here.' % Person.population
> >
> > swaroop = Person('Swaroop', 'M')
> > swaroop.sayHi()
> > swaroop.howMany()
> > kalam = Person('Abdul Kalam', 'M')
> > kalam.sayHi()
> > kalam.howMany()
> > cathy = Person('Catherine', 'F')
> > cathy.sayHi()
> > cathy.howMany()
> > swaroop.sayHi()
> > swaroop.howMany()
> >
> > ############# 8< #########################
> >
> > When I run this script, I got the following exception:
> > Exception exceptions.AttributeError: "'NoneType' object has no
> > attribute 'population'" in <bound method Person.__del__ of
> > <__main__.Person instance at 0xb7d8ac6c>> ignored
> >
> > To to newcomer like me, this message doesn't make much sense. What
> > seems weird to me is that, if I change the variable cathy to something
> > else, like cath, or even cat, then the script will finish gracefully.
> > Why "cathy" is not liked?!!
> >
> > Some of you may have recognized that the code is derived from a sample
> > code in Swaroop's "A byte of python".
> >
> > My python is of version 2.5.1, on Ubuntu.
>
>
> That's really weird... it's reproducible on Windows too. It doesn't
> make any sense why the name of the variable would make a difference.
> My guess is you hit some kind of obscure bug.
>
>
> George
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080323/b9ebdb18/attachment.html>


More information about the Python-list mailing list