Bug or wart? You make the call.

Terry Reedy tjreedy at udel.edu
Thu Mar 6 00:14:44 EST 2003


"Paul Miller" <pwmiller1 at adelphia.net> wrote in message
news:2e363c08.0303051256.58c94b08 at posting.google.com...
> 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

Try Spam.__dict__['eggs'] without assignment, as you did with
'Spam.eggs' above and see what type of object you are assigning to.
This should  answer your question.

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

Correct.

>but if I access it through the class __dict__, I can do it.

You are making an invalid assumption.  See note above.

>Which of these is the correct behavior?

Both

Terry J. Reedy






More information about the Python-list mailing list