
If you are not a newbie of Python, I bet you must know Lambda function. I have to say that I like to use the Lambda function in some circumstances. For example, the Pandas data frame apply()
function, map()
or filter()
functions, they are naturally compatible with Lambda functions and make our life easier. We don’t need to literally declare an explicit function. Instead, just define a logic in-line.
However, in this article today, I will say something on the negative side about Lambda functions. I’m not saying stop using it, but definitely, there is a concern. This concern starts from a question I have seen from my younger friend who is studying in one of the Top 20 University in the world.
f = (lambda x: x((lambda x: x(lambda x: x))(x)))(lambda x: x)
Given this function
f
, please tell what is the output off(1)
.
BTW, you’re not allowed to use a Python interpreter, just get the answer by reading and thinking because this is an online quiz with time constraints 🙂
Pre. Some Backgrounds

I believe someone may know how to use Lambda function in some certain scenarios, but may not understand the mechanism. So, let me just simply explain what is a lambda function consists of.

Don’t worry if it is confusing. In fact, we can rewrite the lambda function into the format that we are familiar with.

As we can see, there are two major differences between the lambda function and a normal Python function:
- Lambda function is anonymous, we don’t have to define it with a function name.
- Lambda function can ONLY have one expression, whereas a normal function can have unlimited expressions to define a much more complex series of procedures.
We love lambda functions because sometimes it is just simple and concise. As a Data Scientist, the most frequent scenario that I use lambda functions is probably in conjunction with Pandas data frames apply()
function, because it takes a "function" as the parameter, and a lambda function can just be the "function" as an argument.
df['col'].apply(lambda x: x+1)
That is equivalent to below using a normal function.
def plus_one(x):
return x+1
df['col'].apply(plus_one)
1. Understand the Structure of the Function

Well, don’t forget we have a question as I’ve shown at the beginning of this article. Before we criticise such a ridiculous function, let’s together derive its output first.
Since the function is ridiculously confused, let’s add some indentation and reformat it for easier understanding purposes.

Now, it a little bit clearer. At least, from the illustration above, we can see that there are 4 lambda functions, as well as their boundaries.
The most important is that we need to know that the x
in these lambda function are isolated. In other words, although they are all named x
, but they are not the same thing.
To reduce the confusion, let’s use a
, b
, c
and d
to replace these x
. Also, we know that lambda functions are anonymous, but let’s assign a name to them for the next step. I just want to make the naming convention as simple as possible. Let’s call such a function lambda d: d
as _d(d)
.
Here is a refined illustration.

Please be noticed that the whole complex thing is a function, which is known, and we know that is called f(x)
that can take a number as the parameter.
2. Re-write All Lambda Functions

All right, now we know the structure of the entire function already, but it is still not clear how to solve the function. That’s because lambda functions do not have very good readability sometimes, and in this case, is ridiculously unreadable.
Do you remember we can rewrite any lambda functions into the form of normal Python functions? Let’s do it now.
It is very easy for the function _c(c)
and _d(d)
, because they just simply return the value of their parameters.
def _c(c):
return c
def _d(d):
return d
Then, let’s look at the function _b(b)
. This one start to be tricky, but don’t forget that everything before the colon :
is the parameter, while everything after that is the body of the function. No matter how the parameter appears in what form, it is the parameter, even though it can be a function.
In other words, the parameter can be a function.
def _b(b):
return b(_c)
Yes, the function _b(b)
is equivalent to say: "Hey! give me a function b(x)
, and I will pass _c
as the parameter x
.
Similarly, although it is more complicated, we can re-write the function _a(a)
and finally, f(x)
.
def _a(a):
return a(_b)(a)
def f(x):
return _a(_d)(x)
Let’s put everything together before we go the final step.
3. Derive the Output of f(x)

By deriving the output without actually running it, we can say that we completely understand it (I believe the instructor who created this question would say something like this).
Now, we have all the functions above, so we can start deriving now. Since it is asked to derive f(1)
, so the function f(x)
will have its x = 1
.

Don’t start to deal with _d
at this stage. It is obvious that _a(_d)
will be a function, and the number 1
is the parameter of it. So, we need to follow the principle "from left to right".

In this step, we expanded the function _a(a)
, and the parameter a
is replaced with the function _d
. So, we ended up with a new function. Now, we need to plug-in the parameter _b
of the function _d(d)
.

The expression _d(_b)
is simply _b
because the function _d
has been defined to return the original parameter whatever passed in. The next step is to expand _b
.

The function _b
takes a function b
as its parameter, in which the argument passed in is _d
in this case. The function as a parameter passed in will have _c
as parameter. The next step is easy since we got _d
again.

Again, _d
will return whatever passed in without any change. Finally, we got the expression _c(1)
. The function _c(c)
as shown below will also simply return anything passed in, just like the function _d(d)
.
def _c(c):
return c
So, it is obvious that _c(1) = 1
.
Therefore f(1) = 1
.
Some Thoughts

Thank you very much if you read through the article until here. Let’s have a deep breath now. I know it is even difficult to read the solution, then how about solving the question? Also, the question is for a University student rather than a very experienced developer? Maybe, even an experienced Python developer like myself need to spend some time to figure it out.
Now, I know why Guido van Rossum (the creator of Python) want to get rid of the lambda function [1]. It is indeed a WoW thing when it solves problems with a single-line expression of functions. At the same time, it can also be evil if one uses it confusingly. The example shown in this article is just an extreme sample but we can imagine that more or less it could happen in practice that some inexperienced programmers want to show off their "skills".
Finally, I understand that an examination of a university subject must have some difficult questions because I used to be part of it, as I stated in this article:
However, I have to say that such a question that is demonstrated in this article, has almost no value to the students. Additionally, it may have negative impacts on the students. For example, that kind of guy I mentioned, who used to "show off" with this kind of "skill" and writing this kind of confusing code might get the "skill" from the University, via such kinds of questions.
If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)
Reference
[1] G., Rossum. The fate of reduce() in Python 3000. All Things Pythonic. Retrieved https://www.artima.com/weblogs/viewpost.jsp?thread=98196 on October 4, 2020