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



The following code has me mystified:

In [4]: class A(object):
...: def show(self):
...: print 'hello'
...:
...:
In [5]: a = A()
In [6]:
In [7]: x = a.show
In [8]: y = getattr(a, 'show')
In [9]: x
Out[9]: <bound method A.show of <__main__.A object at 0xb557d0>>
In [10]: y
Out[10]: <bound method A.show of <__main__.A object at 0xb557d0>>
In [11]:
In [12]: id(x)
Out[12]: 12419552
In [13]: id(y)
Out[13]: 12419872
In [14]:
In [15]: x is y
Out[15]: False
In [16]:
In [17]: x()
hello
In [18]: y()
hello

Basically, the above code is saying that foo.foobar is not the same as
getattr(foo, 'foobar').

But the documentation at
http://docs.python.org/lib/built-in-funcs.html#l2h-33
says that they are equivalent.

And, the following seems even worse:

>>> id(getattr(a, 'show')) == id(a.show)
True
>>> getattr(a, 'show') is a.show
False

What gives? This breaks my understanding of id(), the is operator, and
getattr().

Can someone help me make sense of this?

I'm using Python 2.5.2.

- Dave

--
Dave Kuhlman
http://www.rexx.com/~dkuhlman

.



Relevant Pages

  • Problem with Lexical Scope
    ... I am not completely knowledgable about the status of lexical scoping in ... Python, but it was my understanding that this was added in a long time ... def collect: ...
    (comp.lang.python)
  • Simple newbie question about parameters handling in functions (or am I dump or what?)
    ... I am totaly new to Python, although I have quite strong developer ... function can change wathever parameters reffers to. ... presonal not understanding comes from this) ... def ChangeList: ...
    (comp.lang.python)
  • Re: Use empty string for self
    ... argument but understanding that it's not really something that is always ... I'm trying to train myself to see ... def doittoitas def doittoit ... Python by analogy, but don't fall into the trap of trying to write C++ ...
    (comp.lang.python)
  • Re: Can I reference 1 instance of an object by more names ? rephrase
    ... Stef Mientki wrote: ... when I've some more understanding of these Python ... def fset: ...
    (comp.lang.python)
  • text adventure question
    ... I am working on a text adventure game for python to get back into ... def character_sheet: ... global reputation ... print "Please choose another command. ...
    (comp.lang.python)