Does python hate cathy?

Carsten Haese carsten at uniqsys.com
Mon Mar 24 02:02:31 CET 2008


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 Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list