Interesting problem - can it be done

Roman Suzi rnd at
Thu Aug 9 08:28:19 CEST 2001

On Thu, 9 Aug 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
>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()

Hmmm... First of all, you have methods, not functions.
Second, where from are you getting function's

In Python you can have list of functions (methods) like this:

funclist = [func1, func2, func3]

# and iterate thru it this way:
for f in funclist:
  # use function f here in some way

Anyway, I do not follow why keeping separate and symbolic
list of function. Maybe you can have that list as a
methods of testobj? Or do you want latest-possible binding?
Python is dynamic enough to implement any variant.

>Any help would be great.
>    Mark Sass

Sincerely yours, Roman Suzi
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at _/
_/ Thursday, August 09, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "NEWS! Stolen painting found by tree" _/

More information about the Python-list mailing list