Get bound method by name
Steve Holden
steve at holdenweb.com
Tue Mar 3 15:54:19 EST 2009
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):
y = x.__getattr__("bar")
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list