[Tutor] How to put my functions in an array

Jacob S. keridee at jayco.net
Sat Jan 1 22:10:36 CET 2005


Hello.

    I believe it was Danny Yoo who told me about mapping functions a while
back on the list...
It goes along these lines...

funct = {'Add Virt':addvirt,'Remove Virt':remvirt,'More
Stuff':more,"Extras":extra}
def addvirt():
    pass
def remvirt():
    pass
def more():
    pass
def extra():
    pass
def dispatch(name):
    if name:
        funct['name']()
    else:
        pass

print """\
Actions include:
Add Virt
Remove Virt
More Stuff
Extras
quit
"""
while 1:
    command = raw_input("What do you want to do? ")
    if command == 'quit':
        break
    dispatch(command)


HTH,
Jacob Schmidt

> def addvirt():
> pass
> def remvirt():
> pass
>
> PROVISION_ACTIONS=[('addvirt','Add Virt'),('remvirt','Remove Virt'),]
> formhandlers={}
>
> # this works
> formhandlers["addvirt"]=addvirt
> formhandlers["remvirt"]=remvirt
>
> # this does not work:
> for verb,verb_desc in PROVISION_ACTIONS:
> if callable(verb):
> formhandlers[verb]=verb
>
> I tried a few different syntaxes but to no avail... do I need things
> like: getattr()?
>
> Thanks alot
> Mohamed~
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



More information about the Tutor mailing list