Pythonic Magic: Lambda expressions

All Justin Bieber needs is a Beauty and a Beat. Personally, I am happy with a Beauty and Python. Yeah, honestly, python offers one so much, that it’s difficult to ask for more (except for speed maybe, but that’s a tradeoff for python’s amazing expressiveness. And people try to address it as we speak).

So, as you may already have understood, in this second article at my featured series of Pythonic Magic, I am going to talk about Lambda Expressions. But what are Lambda Expressions, one might wonder. Lambda expressions are one of the most powerful (in my opinion) features of python, but people easily tend to misunderstand it or ignore it all together. Which is bad. ‘Cause it is awesome!

Lambda expressions are essentially a language construct that allows one to create anonymous (that is, not bound by any name) functions (more rather function objects) at runtime. That’s it! Really! I will admit that it’s implementation may be hard for some new python programmers to grasp, but once you get the hold of it, it’s really not that difficult. Before we go any further, it would be useful to provide an example of what a Lambda Expression looks like.

Supposing we want to create a function that returns the square of its input. In python, we would do something like this:

>>> def square(x):
...     return x * x

Pretty straight forward if you ask me. Nothing magical in this. Now let’s take a look at the output the above function produces:

>>> square(3)
>>> square(2.2)

Again, nothing magical (if you ignore that number following the trailing zero’s there, it’s all normal. That behavior is also normal, and has to do with hardware limitations on floating point number calculations.) Now that we got this, it’s time to see how the above could be implemented using Lambda Expressions.

>>> g = lambda x: x ** 2 # ** is the exponent operator in python. I could have also said x * x, but I refrained to do so, for clarity's sake
>>> g
<function <lambda> at 0x7f17656045f0>
>>> g(2)

Very nice if you ask me. As far as the lambda expression is concerned, it can do pretty much anything that the named function can do. And as you can see in my code snippet, using the lambda keyword, you get to have a lambda function object. Having a function object means that the object is callable (I mean obviously, it’s a function), which in python means that you can bind to any reference, and still call it using the parenthesis as in g(2).

If it still is kind of obscure to you, then maybe it should be worth for me to try to explain it a little bit better. Lambda forms have this general form: lambda [parameter list]: expression This is essentially the keyword lambda, followed by a number of parameters, and an expression, the value of which gets returned when you call the lambda function object.

>>> g = lambda x: x ** 2 # In this case x is the parameter, it is used in the expression to calculate the square of it, and the value of the calculated expression (in this case, the square) gets returned when the object is called.

Now I may like to call it with one parameter, but there is really no one stopping you from calling it with more than one parameters. For example take a look at the following

>>> sum = lambda x, y: x + y
>>> sum(4, 2)

>>> # Essentially the same as
... def sum(x, y):
...     return x + y

Of course I barely even touched the matter. However I believe that lambdas are a very powerful feature, which although not terribly useful all the time, may be a killer feature for when you really need that. If you feel like you want to know more about lambda expressions, I formed a neat list with various links explaining more than I am on the matter: