Python: pass statement in lambda form Python: pass statement in lambda form python python

Python: pass statement in lambda form


lambdas can only contain expressions - basically, something that can appear on the right-hand side of an assignment statement. pass is not an expression - it doesn't evaluate to a value, and a = pass is never legal.

Another way of thinking about it is, because lambdas implicitly return the result of their body, lambda: pass is actually equivalent to:

def f():    return pass

Which doesn't make sense. If you really do need a no-op lambda for some reason, do lambda: None instead.


That is an error because after the colon you have to put the return value, so:

lambda: pass

is equal to:

def f():   return pass

that indeed makes no sense and produces a SyntaxError as well.


The return value of a function without a return statement is None. You can see this from the simple pass function that is defined in the OP:

>>> def f():...     pass... >>> print f()None

If you are looking for a lambda function that is equivalent to this "no-op" function, then you can use:

lambda: None

For example:

>>> f = lambda: None>>> print f()None