I'm relatively new to python and I was wondering if it's possible to pass one function to another. I have some functions that basically do the same think like:
if(#some condition):
#do something
else:
#do something else
What I want to do is something like this:
def generic_func(self, x, y):
if #somecondition:
#do function x
else:
#do function y
def calls_generic_func(self, key, value):
lambda x: self.list[-1].set(key,value)
lambda y: self.d.set(key,value)
self.generic_func(x,y)
Unfortunately this doesn't seem to work as when I call self.generic_fun(x,y) I get an error that says global name x is not defined. Any ideas?
calls_generic_funca lambda expression on a line by itself like that is simply thrown away. It does not define x or y, that's possibly whyself.generic_func(x,y)fails.fun = lambda x: x*xis equivalent todef fun(x): (new indented line) return x*xBasicallylambdalets you write one line functions inline, becausedefdoesn't let you do that (unlike javascript'sfunction())