Get bound method by name
Chris Rebert
clp2 at rebertia.com
Tue Mar 3 16:01:08 EST 2009
On Tue, Mar 3, 2009 at 12:54 PM, Steve Holden <steve at holdenweb.com> wrote:
> Graham Breed wrote:
>> Johannes Bauer wrote:
>>> Hello group,
>>>
>>> I'm looking for a Python function but have forgotten it's name.
>>> Essentially what I want is:
>>>
>>> class Foo():
>>> def bar(self):
>>> pass
>>>
>>> x = Foo()
>>> y = x.MAGIC("bar")
>>> print(y)
>>> <bound method Foo.bar of <__main__.Foo instance at 0xb7e11fcc>>
>>>
>>> So the question is: How is the magic function called which returns me
>>> the bound method of a class instance by its name? I know there was a way
>>> but just can't remember...
>>
>> y = getattr(x, "bar")
>
> Or, as a method call (which was requested):
The OP was a bit inconsistent. He only used the word "function", but
his pseudocode used a method.
Either way, one shouldn't normally use magic methods directly.
Cheers,
Chris
--
I have a blog:
http://blog.rebertia.com
More information about the Python-list
mailing list