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?

user3766476
  • 155
  • 1
  • 6
  • possible duplicate of [Passing functions with arguments to another function in Python?](http://stackoverflow.com/questions/803616/passing-functions-with-arguments-to-another-function-in-python) – Paul Nov 01 '14 at 02: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()` ) – Paul Nov 01 '14 at 02:29

1 Answers1

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))
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    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 Nov 01 '14 at 02:09
  • 1
    @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) – falsetru Nov 01 '14 at 02:15
  • @Paul, In Python arguments should match parameters (except arbitrary parameters, keyword parameters) – falsetru Nov 01 '14 at 02:16
  • 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 Nov 01 '14 at 02:19
  • @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? – falsetru Nov 01 '14 at 02:21
  • @user3766476, If items are dictionaries, you need to use `__setitem__` instead of `set`. – falsetru Nov 01 '14 at 02:21