Calling a function dynamically
anton muhin
antonmuhin at rambler.ru
Thu Jan 8 10:53:57 EST 2004
Paradox wrote:
> I would like to call a function whose name I supply at runtime.
> Something like this but it didn't work
>
> functionName = 'run'
> instance = ClassDef()
> args = [1, 2]
>
> #want the dynamic equivalant of
> #instance.run(*args)
>
> #This didn't work cause there was no __call__ attribute. Why?
> value = instance.__call__[functionName](*args)
>
>
>
>
> Thanks Joey
This should work:
getattr(instance, functionName)(*args)
regards,
anton.
More information about the Python-list
mailing list