Interesting problem - can it be done
Johan Jonkers
johan at johanjonkers.com
Thu Aug 9 02:51:19 EDT 2001
Mark Sass wrote:
> Hi all,
> I have a list of strings and the strings are functions that I want to
> call on an object. How do I call the functions without hardcoding the
> function call? Here is an example
>
> funclist = ['func1()', 'func2()', 'func3()']
> x = len(funclist)
> y = 0
> while y < x:
> value = testobj.funclist[y]
> y = y +1
>
funclist = ['func1', 'func2', 'func3']
for func in funclist:
exec('value = testobj.%(func)s()' % locals())
print value
This will iterate the the list of functions and will call them and store
the return value in the variable called 'value', which can be used as a
regular variable after the exec-call
> This way I could grow the list of functions to be called without coding
> more like the way I am doing it now. I would like to avoid doing the
> following:
>
> value = testobj.func1()
> value = testobj.func2()
> value = testobj.func3()
>
> Any help would be great.
>
> Thanks,
> Mark Sass
>
Johan Jonkers
More information about the Python-list
mailing list