[Tutor] Iterating over list of functions

Robert Berman bermanrl at cfl.rr.com
Wed May 20 16:52:08 CEST 2009


Thank you, Emile.

That is the exact answer I needed.

Robert


On Wed, 2009-05-20 at 07:48 -0700, Emile van Sebille wrote:

> On 5/20/2009 7:25 AM Robert Berman said...
> > Hi,
> > 
> > Given a list of options: option_1.......option_n. For each option I have
> > a corresponding function: func_1..... func_n. I have all function names
> > defined in a list similar to flist = [func_1, func_2,.......func_n]
> > which I know is a legitimate construct having found a similar  construct
> > discussed by Kent Johnson in 2005.
> > 
> > What I do not know how to do is to call the selected function. If the
> > index of options is 1, then I want to call func_2; do I code
> > flist[index]?
> 
> Yes - with trailing () of course...
> 
> def func1():return 1
> def func2():return 2
> def func3():return 3
> flist = [func1,func2,func3]
> flist[0]()
> flist[1]()
> flist[2]()
> 
> Emile
> 
> 
>   I do not think Python has a branch indirect construct so I
> > cannot use anything similar to that methodology.  What is the best
> > approach to take to solve this problem?
> > 
> > Thank you for any assistance, hints, solutions, and guidelines.
> > 
> > Robert
> > 
> > 
> > 
> > 
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> > 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090520/0a8db103/attachment-0001.htm>


More information about the Tutor mailing list