Given one of those:
def operations(x, y, z):
def add(x,y):
return x+y
def sub(x,y):
return x-y
return z(x,y)
--------------------------------------------------
def operations(x, y, z):
if z == add:
def add(x,y):
return x+y
if z == sub:
def sub(x,y):
return x-y
res = z(x,y)
return res
I'm trying to call one of multiple inner functions from one of the outer variables function in python but I get this errors:
result = operations(10,5,add)
=>
NameError: name 'add' is not defined
--------------------------------------------------
result = operations(10,5,"add")
=>
TypeError: 'str' object is not callable
I know i could use this solution:
def add(x,y):
return x+y
def sub(x,y):
return x-y
def operations(x, y, z):
return z(x,y)
But for me it seems clearer to use nested functions.
I also read this: Short description of the scoping rules? But it didn't really helped me.