Reputation: 13
Is it possible to pass arguments to a function within a dictionary?
d = {"A": foo(), "B": bar()}
def foo(arg):
return arg + 1
def bar(arg):
return arg - 1
In this case I want to pass arg to bar() by referencing the function dynamically
d["B"] #<-- pass arg to bar()
Upvotes: 1
Views: 74
Reputation: 49318
It's possible, but you have to refer to the function (e.g. foo
) without already calling it (e.g. foo()
).
def foo(arg):
return arg + 1
def bar(arg):
return arg - 1
d = {"A": foo, "B": bar}
Result:
>>> d['A'](5)
6
Upvotes: 2