Take the 2-minute tour ×

Now and then I use the Python lambda. Is it so formal that it is safe to say that you can do formal lambda calculus with it? I just used it but I didn't fully understand whether the python lambda and the lambda calculus like I read was done by Alonzo Church. I also used it in Javascript, I think. Isn't this more common in functional languages (e.g. Haskell, Scheme/Lisp, Clojure...) and I never saw lambda in use with Java or C(++)?

Is says on this site " a function you can pass on to another function as argument.".

What is "lambda" code?

But how do I get used to it enough so that I can tell where to benefit from it? If I can do it in say SQL, JPQL or GQL instead, shouldn't I prefer to do it in the query language?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Python lambda expressions are real, formal untyped λ-calculus lambda expressions.

They fit the formal definition; they can only represent one python expression, based on variables (free or otherwise) and references to other functions (abstract symbols). Python uses parenthesis in expressions too.

You use them wherever a lambda is more suitable and convenient than a full function definition. The python def functionname(argumentlist): syntax forms a statement; in Python you cannot put statements inside of expressions, only the other way around. A lambda on the other hand, is an expression, so you can use a lambda to insert a callback function inline:

map(lambda x, y: x[y+5], [(mapping1, integerkey1), (mapping2, integerkey2)])

The above example consists only of an expression. The python map() function takes, as its first argument, a callable, which is applied to each and every element in the list given by the second argument. In the above example, using a lambda expression to define that callable is much easier than using a function statement:

def mapcallback(x, y):
    return x[y + 5]

map(mapcallback, [(mapping1, integerkey1), (mapping2, integerkey2)])

For the full function syntax I need to assign a name, put the function definition on separate lines, and use the return statement to return the result of the expression.

share|improve this answer
"Python lambda expressions are real, formal λ-calculus lambda expressions.": I am not an expert of lambda calculus, but I think one should specify if you mean untyped or typed lambda calculus and, if typed lambda calculus is meant, which variation of it (as far as I know, there are more than one). Alternatively, one could add a reference (in the literature) to the formal definition of lambda calculus which is implemented in Python. –  Giorgio Feb 5 '13 at 9:52
@Giorgio: Added 'untyped' (python itself is untyped, no formal restrictions are made as to the types you call the lambda with) and a link to the Wikipedia formal definition. –  Martijn Pieters Feb 5 '13 at 13:38
Pieter: I would say that Python is dynamically typed rather than untyped. To my knowledge, this is not the same. E.g. in untyped lambda calculus you cannot distinguish between a function, a boolean, and a natural number. –  Giorgio Feb 5 '13 at 15:57
@Giorgio: I didn't say Python was untyped; I said that python lambdas are untyped lambdas as described in the WP article: Another aspect of the untyped lambda calculus is that it does not distinguish between different kinds of data. For instance, it may be desirable to write a function that only operates on numbers. However, in the untyped lambda calculus, there is no way to prevent a function from being applied to truth values, strings, or other non-number objects. Which is exactly how it works in Python. –  Martijn Pieters Feb 5 '13 at 16:02
Caveat: I am not an expert neither in lambda calculus nor in Python. What I meant is that in untyped lambda calculus variables in a lambda expression can be bound to values that do not have any type. In Python, variables in a lambda expression can be bound to values of any type, but they do get a type once they are bound. That's why you can execute (lambda x, y: x + y)(1, 2) and (lambda x, y: x + y)("A", "B"), but (lambda x, y: x + y)(1, "A") gives an error. In untyped lambda calculus the last expression would be valid because there is no distinction between truth numbers and strings. –  Giorgio Feb 6 '13 at 14:23

Your Answer


