Using a tuple I would like to autorun a function passing the rest of the tuple as parameters. It’s easier to understand what i’m trying to do if i show you what i have so far:
def Function1(var1,var2,post=False):
if post: print "Function One "+str(var1)+str(var2)
return "FN1"
def Function2(var1,var2,var3,var4,post=False):
if post: print "Function Two "+str(var1)+str(var2)+str(var3)+str(var4)
return "FN2"
pattern = ( ('Function1',1,2) , ('Function2',1,2,3,4) )
IDs = []
#the next line is the line i can't get working, i[0] is successfully calling
#but i can't get the tuple to pass (var,var,True)
#instead it passes to the function as ((var,var),True)
for i in pattern: IDs += eval(i[0])(i[1:],True)
print IDs
I want my output to be:
'''
Function One 12
Function Two 1234
["FN1","FN2"]
'''
You need three things:
arguments.
the magic star.
strings properly.
So:
Mandatory
eval()Lecture: Experienced Python programmers considereval()to be a dangerous last resort. You should look up functions by name in the local module’s dictionary instead:It’s even cleaner and easier if you can restrict the functions to be executed by name to methods within a class object; you need only reference
yourObject.__dict__[ functionName ].