1

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?

2
  • possible duplicate of Passing functions with arguments to another function in Python? Commented Nov 1, 2014 at 2:23
  • In calls_generic_func a lambda expression on a line by itself like that is simply thrown away. It does not define x or y, that's possibly why self.generic_func(x,y) fails. fun = lambda x: x*x is equivalent to def fun(x): (new indented line) return x*x Basically lambda lets you write one line functions inline, because def doesn't let you do that (unlike javascript's function() ) Commented Nov 1, 2014 at 2:29

1 Answer 1

2

x in the following expression is a parameter, not a function (lambda).

lambda x: self.list[-1].set(key,value)

You need to assign the lambda expression to a variable and pass that.

function1 = lambda x: self.list[-1].set(key,value)
function2 = lambda y: self.d.set(key,value)
self.generic_func(function1, function2)

Or you can pass the lambda expression itself:

self.generic_func(lambda x: self.list[-1].set(key,value),
                  lambda y: self.d.set(key,value))

If you meant the functions do not take any parameter, remove them (x, y).

self.generic_func(lambda: self.list[-1].set(key,value),
                  lambda: self.d.set(key,value))
Sign up to request clarification or add additional context in comments.

6 Comments

Do you think something might be wrong with the lambda expressions, seeing as how the left hand parameters x and y are never used in the right hand side for the result? The syntax is allowed, sure. A function is allowed to ignore its parameters. But functionally it is not a good sign.
@Paul, It depends on the situation. For example, if caller calls with event object, you can ignore it. (BTW, I added mention about parameter in the answer)
@Paul, In Python arguments should match parameters (except arbitrary parameters, keyword parameters)
So when I do it with a lambda function my set command doesn't seem to work correctly. The only thing I'm changing is making is a lambda expression instead of explicitly doing it. Any ideas why that might be happening?
@user3766476, Without knowing what the list self.list contain, I can't answer that. Is there set method in the object contains in the list?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.