dynamic invoke

Sushant truthismidway at aim.com
Fri Oct 19 10:29:12 EDT 2007


Python will not allow string to be used a function pointer. It is type unsafe. 
Best way is to convert string into function pointers manually.

if dynamicMethod == 'bar':
    method = oFoo->bar
else:
    method = oFoo->default
method()

On Friday 19 October 2007 7:56 am, Diez B. Roggisch wrote:
> Nils wrote:
> > On Oct 19, 12:39 pm, lukasz.... at gmail.com wrote:
> >> Hello,
> >>
> >> Is there any way (other then eval) to invoke a method by passing
> >> method name in a string.
> >> It's very simple in php:
> >> $oFoo = new Foo();
> >> $dynamiMethod = "bar";
> >> $oFoo->$dynamiMethod();
> >>
> >> Unfortunately I can't find a good solution to do the same thing in
> >> python. Does it have some build-in function to do it?
> >>
> >> Kind Regards,
> >>
> >> Lukasz.
> >
> >  Use apply():
> > http://docs.python.org/lib/non-essential-built-in-funcs.html
>
> Apply doesn't help the OP - it's useful if you already have a function
> pointer.
>
> And it's obsoleted by the * and ** parameter passing mechanism.
>
> Diez



More information about the Python-list mailing list