By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

calling variable function name ?

P: n/a
I don't know the exact terminology in python, but this is something i
am trying to do

i have 3 functions lets say
FA(param1,param2)
FB(param1,param2)
FC(param1,param2)

temp = "B" #something entered by user. now i want to call FB. I don't
want to do an if else because if have way too many methods like
this...

var = "F" + temp
var(param1, param2)

This does not work ofcourse. Does anyone know how to implement this ?

Apr 8 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Apr 8, 8:52*pm, TkNeo <tarun....@gmail.comwrote:
I don't know the exact terminology in python, but this is something i
am trying to do

i have 3 functions lets say
FA(param1,param2)
FB(param1,param2)
FC(param1,param2)

temp = "B" #something entered by user. now i want to call FB. I don't
want to do an if else because if have way too many methods like
this...

var = "F" + temp
var(param1, param2)

This does not work ofcourse. Does anyone know how to implement this ?
F = { 'A': FA, 'B':FB, 'C':FC }

F['A'](param1, param2)

HTH

--
Arnaud

Apr 8 '08 #2

P: n/a
On Apr 8, 3:52 pm, TkNeo <tarun....@gmail.comwrote:
I don't know the exact terminology in python, but this is something i
am trying to do

i have 3 functions lets say
FA(param1,param2)
FB(param1,param2)
FC(param1,param2)

temp = "B" #something entered by user. now i want to call FB. I don't
want to do an if else because if have way too many methods like
this...

var = "F" + temp
var(param1, param2)
Try this:

func = globals()["F" + temp]
func(param1, param2)

HTH,
George
Apr 9 '08 #3

P: n/a
On Apr 8, 7:51 pm, George Sakkis <george.sak...@gmail.comwrote:
On Apr 8, 3:52 pm,TkNeo<tarun....@gmail.comwrote:
I don't know the exact terminology in python, but this is something i
am trying to do
i have 3 functions lets say
FA(param1,param2)
FB(param1,param2)
FC(param1,param2)
temp = "B" #something entered by user. now i want to call FB. I don't
want to do an if else because if have way too many methods like
this...
var = "F" + temp
var(param1, param2)

Try this:

func = globals()["F" + temp]
func(param1, param2)

HTH,
George


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'

Jun 27 '08 #4

P: n/a
TkNeo <ta*******@gmail.comwrites:
>
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
Jun 27 '08 #5

P: n/a
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" <ar*****@googlemail.comwrote
in message news:m2************@googlemail.com...
TkNeo <ta*******@gmail.comwrites:
>>
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
Jun 27 '08 #6

P: n/a
"thinkofwhy" <th********@yahoo.cawrote:
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
Python has a neat concept for making this easy :^) it is called a class.

class MyFunctions(object):
def funcA(self, param1, param2):
print "FA " + param1 + " " + param2

def funcB(self, param1, param2):
print "FB " + param1 + " " + param2

def funcC(self, param1, param2):
print "FC " + param1 + " " + param2

def defaultFunc(self, *args):
print "Command not recognised"

def doCommand(self, cmd, *args):
return getattr(self, 'func'+cmd, self.defaultFunc)(*args)

functions = MyFunctions()
result = functions.doCommand('A', 'foo', 'bar')

You have to add an extra 'self' argument to each function (or make it a
staticmethod), but you could always use it to store state without resorting
to globals.
Jun 27 '08 #7

P: n/a
On Apr 30, 11:05 am, Arnaud Delobelle <arno...@googlemail.comwrote:
TkNeo<tarun....@gmail.comwrites:
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
oh crap. I can't believe I was doing that ! :) Thanks for pointing
out. works perfect.

Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.