Does python hate cathy?

George Sakkis george.sakkis at gmail.com
Sun Mar 23 20:42:21 EDT 2008


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



More information about the Python-list mailing list