Robert Berman wrote:

Thank you, Christian. This solution was one I was not expecting and am
glad to receive it. It is one I will explore in greater detail later.

Robert


On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote:
Robert Berman wrote:
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]? 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@python.org
http://mail.python.org/mailman/listinfo/tutor

Why not use a dictionary to do the heavy lifting for you

 >>> import string
 >>> funcs = {1:string.upper, 2:string.lower}
 >>> funcs[1]('this is a simple test')
'THIS IS A SIMPLE TEST'
 >>> funcs[2]('THIS IS A SIMPLE TEST')
'this is a simple test'


(Your top-posting makes the thread hard to follow)

Note that once it's a dictionary, you can use whatever keys you would normally use in a dictionary. For example, if the options are strings, use a string as the key. If they literally are numbers, then a list is preferable, but a dictionary gives you other choices.

DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to