Bug or wart? You make the call.

Paul Miller pwmiller1 at adelphia.net
Wed Mar 5 15:56:53 EST 2003


Just out of curiosity, I tried something like this: 

Python 2.2 (#1, Apr 12 2002, 15:29:57)
[GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Spam (object):
...     def eggs (self):
...             pass
...
>>> Spam.eggs
<unbound method Spam.eggs>
>>> Spam.eggs.attr = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'instance method' object has no attribute 'attr'
>>> spam = Spam()
>>> spam.eggs.attr = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'instance method' object has no attribute 'attr'
>>> spam.__dict__
{}
>>> Spam.__dict__['eggs'].attr = 1
>>> spam.eggs.attr
1
>>> Spam.eggs.attr
1
>>>

In other words, when I try to set a nonexistent attribute of a method,
either in a class or an instance, I get an AttributeError, but if I
access it through the class __dict__, I can do it.  Which of these is
the correct behavior?  Incidentally, trying the same thing on a fresh
Spam instance gives, as expected, a KeyError: eggs.




More information about the Python-list mailing list