Does python hate cathy?

2008-03-23 Thread QS

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 > 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.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does python hate cathy?

2008-03-23 Thread QS
Thanks to you all! It's good to know...

On Mar 23, 9:02 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Sun, 2008-03-23 at 17:42 -0700, George Sakkis wrote:
> > 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.
>
> This is not a bug, just an unexpected 
> feature:http://mail.python.org/pipermail/python-list/2005-January/304873.html
>
> What's happening is that at the end of the script, all objects in the
> global namespace are set to None (in order to decrease their reference
> count and trigger garbage collection). This happens in the order in
> which the names appear as keys in the globals dictionary. It randomly
> happens that "swaroop", "kalam", and "cath" all are hashed in front of
> "Person", but "cathy" is hashed after "Person".
>
> Hence, if Catherine is named cath, Python "None's" all the instances
> first and then the type last, and all is well. However, if Catherine is
> called cathy, Person is set to None before cathy. Then, in the lookup of
> the global name "Person" during cathy.__del__, Person is None, which
> doesn't have a "population" attribute, causing the AttributeError.
>
> Possible workarounds are:
> 1) Explicitly delete the global names for the instances before the
> script runs out.
> 2) Don't refer to the "Person" type by its global name in __del__, but
> indirectly as type(self). (This requires Person to be a new-style class,
> though.)
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list