Get bound method by name

Chris Rebert clp2 at rebertia.com
Tue Mar 3 06:18:42 EST 2009


On Tue, Mar 3, 2009 at 3:12 AM, Johannes Bauer <dfnsonfsduifb 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")

getattr() is the function you seek.

y = getattr(x, "bar")

> print(y)
> <bound method Foo.bar of <__main__.Foo instance at 0xb7e11fcc>>

Cheers,
Chris

-- 
Shameless self-promotion:
http://blog.rebertia.com



More information about the Python-list mailing list