Lambdas in conditions

Hello everyone this is more of a general programming question. I am trying to understand why VC uses lambda keyword in condition functions. for example this script runs fine.

a = 1
b = 5

def logic(x,y): 
    if x < y:
      return True
    else:
      return False

def OnRun():
  print "test"
  print "is ",a," less than ",b
  print logic(a,b)
  condition(lambda: logic(a,b))
  print "passed"

but if I remove the lambda key word it will not get past the condition function. I really don’t understand why this is.

the logic function returns a value of true or false, and the condition function requires a value of true or false in order to resume the script running so why does it need the lambda keyword.

I read up alot on lambda in python but all I seem to find is that is used for anonymous functions.
that is to say functions without names with out using the “def” keyword.

ex

lambda arguments : expression

but here in VC components we define a function of name logic.
and then we use lambda to call that function by it’s name. it just doesn’t make sense to me why.

if anyone can shed some light I would greatly appreciate it.

Thanks

Hi,

I think that the difference is that without lambda ( condition(logic(a,b)) ) you pass the return value of that function into condition so either True or False. And what condition expects is a function handle instead and with lambda you pass a function and not a boolean ( condition(lambda: logic(a,b)) ). If your function didn’t take arguments you could write condition like this: condition(logic). In order to pass a function with arguments to another function lambda is the simplest way to do as far as I know. This stackoverflow thread discusses this topic: https://stackoverflow.com/questions/803616/passing-functions-with-arguments-to-another-function-in-python

-k

2 Likes