Bug or wart? You make the call.

Jp Calderone exarkun at intarweb.us
Fri Mar 7 15:18:03 EST 2003


On Fri, Mar 07, 2003 at 10:05:24AM -0800, Inyeol Lee wrote:
> On Thu, Mar 06, 2003 at 12:14:44AM -0500, Terry Reedy wrote:
> > 
> > [snip]
> > 
> > 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?

  There is one very practical reason.  Method objects (not "class methods" -
those are something else) are created anew every time they are fetched!  Any
attributes you set on them won't be there the next time you grab the method
object.

  You can see this behavior for yourself:

    >>> class foo:
    ...   def bar(): pass
    ...
    >>> f = foo()
    >>> a, b = f.bar, f.bar
    >>> id(a), id(b)
    (136028180, 136028220)

  To toss in my 2c, state belongs on "f" anyway, not on f's methods.

  Jp

-- 
 up 4 days, 11:59, 8 users, load average: 0.00, 0.03, 0.05





More information about the Python-list mailing list