[portland] Using Dictionary to Call Functions
kirby urner
kirby.urner at gmail.com
Tue Jan 15 02:32:45 CET 2008
> I want to get the dictionary keys in sequential order (there may be 1 to
> 7), and call the appropriate function for each one. The functions use
> mathplotlib to draw the curves on a common set of axes.
Hi Rich --
As the gurus here will tell you, dictionaries are by design ignorant of
sequence, though using keys 1-7 makes it easy for a key press to
access a function or whatever.
A typical idiom in a 1970s style menu-driven program would be:
myfuncs = [exit, foo, bar]
while True:
print """
====
Main Menu
1 do foo
2 do bar
0 exit
"""
somenum = int( raw_input("Wanna do? ") )
# whatever error trapping
if somenum == 0: break
myfuncs[somenum]() # take action! (and loop to menu)
That's very simplified and doesn't address your
wanting to pass arguments etc.
myfuncs = {0:exit, 1:foo, 2:bar} # dictionary instead of list
would pretty much serve the same purpose.
HTH,
Kirby
>
> Suggestions and thoughts welcomed.
>
> Rich
>
> --
> Richard B. Shepard, Ph.D. | Integrity Credibility
> Applied Ecosystem Services, Inc. | Innovation
> <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland
>
More information about the Portland
mailing list