a Python bug in processing __del__ method ??
Steven Bethard
steven.bethard at gmail.com
Mon Jan 31 02:06:11 EST 2005
Baoqiu Cui wrote:
> Today I was playing with a small Python program using Python 2.4
> on Cygwin (up-to-date version, on Windows XP), but ran into a
> strange error on the following small program (named bug.py):
>
> -------------------------------
> #!/usr/bin/python
>
> class Person:
> population = 0
> def __del__(self):
> Person.population -= 1
>
> peter = Person()
> -------------------------------
>
> The error returned is this:
>
> $ python bug.py
> Exception exceptions.AttributeError: "'NoneType' object has no
> attribute 'population'" in <bound method Person.__del__ of
> <__main__.Person instance at 0xa0c9fec>> ignored
>
> However, if I rename variable name 'peter' to something like 'peter1'
> or 'david', the error is gone. Looks to me the
> error only happens to variable name 'peter'.
>
> Does anyone know what is wrong? Is this a bug only on Cygwin?
I get this error on a normal Python WinXP install too. Looks like
Python deletes globals in order when exiting, so the Person class gets
deleted before the Person instances:
---------- bug.py ----------
class Person(object):
population = 0
def __del__(self):
Person.population -= 1
<name> = Person()
print list(globals())
----------------------------
I tried this with a few different names. Here are the results for
'apple', 'banana' and 'cranberry'. Only when the name appears after
'Person' in the globals do I get the error that you get.
[D:\Steve]$ bug.py
['apple', '__builtins__', '__file__', 'Person', '__name__', '__doc__']
[D:\Steve]$ bug.py
['__builtins__', '__file__', 'banana', 'Person', '__name__', '__doc__']
[D:\Steve]$ bug.py
['__builtins__', '__file__', 'Person', 'cranberry', '__name__', '__doc__']
Exception exceptions.AttributeError: "'NoneType' object has no attribute
'population'" in <bound method Person.__del__ of <__main__.Person object
at 0x009D1A50>> ignored
I don't know whether this is a bug or a "feature". ;)
Steve
More information about the Python-list
mailing list