call method by name?

Brian Quinlan brian at sweetapp.com
Thu Jan 24 19:58:54 EST 2002


Chris Liechti wrote:
> > How to say this in Python?
> >
> > method_name = 'method1'
> > a. at method_name()
> 
> apply(a, method_name)

This won't work. apply expects a callable as its first argument and a
parameter list as its second argument. If you wanted to be verbose, you
could write it as:

apply(getattr(a, method_name),())

But the apply is really redundant:

getattr(a, method_name)()

Cheers,
Brian





More information about the Python-list mailing list