getattr(foo, 'foobar') not the same as foo.foobar?

Arnaud Delobelle arnodel at googlemail.com
Thu Mar 13 19:45:49 EDT 2008


On Mar 13, 11:29 pm, Dave Kuhlman <dkuhl... at rexx.com> wrote:
> Arnaud Delobelle wrote:
>
> > 4.  Both points above follow from the fact that foo.bar is really a
> > function call that returns a (potentially) new object: in fact what
> > really happens is something like
>
> Arnaud and Imri, too -
>
> No.  foo.bar is *not* really a function/method call.

It is.  The keyword here is 'descriptor'.  Maybe reading this will
help:

http://users.rcn.com/python/download/Descriptor.htm

>
>
> >     Foo.__dict__['bar'].__get__(foo, Foo).
>
> > So every time foo.bar is executed an object is (or may be) created,
> > with a new id.
>
> > HTH
>
> I appreciate the help, but ...
>
> Actually, it does not help, because ...
>
> My understanding is that foo.bar does *not* create a new object.  All it
> does is return the value of the bar attribute of object foo.  What new
> object is being created?

A bound method.

Compare the following:

>>> foo.bar
<bound method Foo.bar of <__main__.Foo object at 0x69850>>
>>> Foo.__dict__['bar'].__get__(foo, Foo)
<bound method Foo.bar of <__main__.Foo object at 0x69850>>
>>>

> If I have:
>
>     class Foo(object):
>         def bar(self): pass
>
> And I do:
>
>     foo = SomeClass()
>
> then:
>
>     foo.bar
>
> should return the same (identical) object everytime, no?  yes?

No.  This is what I explained in my original reply.

> I'm still confused.

That's because you need to adjust your understanding.

--
Arnaud




More information about the Python-list mailing list