Is there a simple way in python to pass a function requiring args, as a parameter without args, and add te args in the destination function? I'm thinking of something like:
def add(a,b):
return a b
def multiply(a,b):
return a * b
def destination(variable, func):
c = 5
d = 3
e = func(c , d) * variable
return e
s = 10
output1 = destination(s, add())
output2 = destination(s, multiply())
CodePudding user response:
()
is actually an operator that means "call this object" when suffixed to a name. Everything between the parens is interpreted as an argument list. The object can be a function or an instance of a different class that implements the magic __call__
method.
When you do add()
, you're calling the add
function with no arguments. But the name add
itself is just another object in python. You can pass it around, use it as any other name, and call it with an argument list.
CodePudding user response:
You've got the right idea but instead of passing the function into destination as an argument, you're trying to call it and pass the return value as an argument instead.
Try this: output1 = destination(s, add)