calling variable function name ?

thinkofwhy thinkofwhy at yahoo.ca
Wed Apr 30 14:58:32 EDT 2008


Try a dictionary:

    def funcA(blah, blah)
    def funcB(blah, blah)
    def funcC(blah, blah)
    functions = {'A': funcA, 'B': funcB, 'C': 
funcC}
    user_func = 'A'
    functions[user_func] #execute function


"Arnaud Delobelle" <arnodel at googlemail.com> wrote 
in message news:m28wyvba54.fsf at googlemail.com...
> TkNeo <tarun.kap at gmail.com> writes:
>
>>
>> George - Thanks for your reply but what you 
>> suggested is not working:
>>
>> def FA(param1,param2):
>>     print "FA" + param1 + " " + param2
>> def FA(param1,param2):
>>     print "FB" + param1 + " " + param2
>> def FA(param1,param2):
>>     print "FC" + param1 + " " + param2
>>
>> temp = sys.argv[1]
>>
>> func = globals()["F" + temp]
>> func("Hello", "World")
>>
>>
>> I ran the script with first parameter as B and 
>> i get the following
>> message
>> KeyError: 'FB'
>
> Perhaps if you call your three function FA, FB, 
> FC instead of FA, FA,
> FA it'll help?
>
> -- 
> Arnaud 




More information about the Python-list mailing list