python - dict of functions with fixed input -


i have functions call through dictionary pass on fixed values.

def dosum(a,b):         print a+b def doprod(a,b):         print a*b 

if pass on input via

d = {'sum': dosum,'prod':doprod} d['prod'](2,4) 

it works fine , prints 8.

but if try

d = {'sum': dosum(2,4),'prod':doprod(2,4)} d['prod'] 

it prints 6 , 8. how can change code run function specify key fixed parameters in dict?

as "old school" alternative martijn's anwser can use lambda functions:

d = {     "sum": lambda: dosum(2, 4),     "prod": lambda: doprod(2, 4),     } d["sum"]() 

Comments