Bug or wart? You make the call.

Inyeol Lee inyeol.lee at siimage.com
Fri Mar 7 13:05:24 EST 2003


On Thu, Mar 06, 2003 at 12:14:44AM -0500, Terry Reedy wrote:
> 
> "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.

I'm a little bit confused. It seems that we cannot add arbitrary
attributes to class methods, but can add them to functions. Is it
correct? Is there any specific reason for this difference?

Inyeol





More information about the Python-list mailing list