python - pass argument to function in a dictionary -
i've switch, given option call function e.g.
#... capture option op = getoption() #.. capture metric mt = getmetric() def zipcode(): print "you typed zero.\n" def desc(): print "n perfect square\n" def address(): print "n number\n" #call desired option options= {0 : zipcode, 1 : code, 2 : desc, 3 : address } options[op]()
i trying pass parameter (mt) options dict call function, i'm not being able so.
if op received 1 , mt foo, how call right function (zipcode) done passing mt parameter?
ideally: options[op](mt)
, defining 1 parameter in function?
thanks
your code not indented properly, important in python , cause syntax errors is.
however, suggesting work perfectly.
consider following:
def multiply(m,n): return n*n def add(m,n) return m,n my_math = { "+":add, "*":multiply}
you call follows:
>>> print my_math["+"](1,2) 3 >>> print my_math["*"](4,5) 20
Comments
Post a Comment