Get bound method by name

John Machin sjmachin at lexicon.net
Tue Mar 3 12:17:30 CET 2009


On Mar 3, 10:12 pm, Johannes Bauer <dfnsonfsdu... at gmx.de> 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...

getattr(x, "bar")




More information about the Python-list mailing list