Does python hate cathy?
John Machin
sjmachin at lexicon.net
Sun Mar 23 20:58:03 EDT 2008
On Mar 24, 11:42 am, George Sakkis <george.sak... at gmail.com> wrote:
> 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.
>
My guess after reading the manual:
"""
Warning: Due to the precarious circumstances under which __del__()
methods are invoked, exceptions that occur during their execution are
ignored, and a warning is printed to sys.stderr instead. Also, when
__del__() is invoked in response to a module being deleted (e.g., when
execution of the program is done), other globals referenced by the
__del__() method may already have been deleted. For this reason,
__del__() methods should do the absolute minimum needed to maintain
external invariants.
"""
is that farnarkeling about in a __del__ method is *not* a good idea.
More information about the Python-list
mailing list