[Somewhat Off Topic] AI Contest

Ken Seehof kseehof at neuralintegrator.com
Mon Aug 13 18:21:39 EDT 2001


>>> class C:
...    def func1(self):
...        print 'executing func1'
...    def func2(self):
...        print 'executing func2'
...    def func3(self):
...        print 'executing func3'
...
>>> obj = C()
>>> funclist = ['func1', 'func2', 'func3']
>>> for f in funclist:
...  r = getattr(obj, f)()
...
executing func1
executing func2
executing func3

- Ken Seehof
kseehof at neuralintegrator.com
www.neuralintegrator.com/kseehof

> Mike Clarkson 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()
> >
> > Any help would be great.
> >
> > Thanks,
> >     Mark Sass






More information about the Python-list mailing list