DEV Community

Nic
Nic

Posted on • Originally published at coderscat.com on

Python: Pros and Cons of Lambda

2020_03_29_python-cons-and-pros-of-lambda.org_20200330_002955.png

Figure 1: Photo by Z S on Unsplash

Basics for lambda

lambda is a keyword in Python, we use it to create an anonymous function. So we also call lambda functions as anonymous functions.

But what’s anonymous functions?

Normal function defined like this:

def sum_two(x, y):
    x + y

print(sum_two)
print(type(sum_two))

#<function sum_two at 0x10f54eb18>
#<type 'function'>
Enter fullscreen mode Exit fullscreen mode

From the result, sum_two is the name of the defined function, it’s type is ‘function’.

Compared to normal function, anonymous function is a function without a name:

print(lambda x, y: x + y)
print(type(lambda x, y: x + y))

#<function <lambda> at 0x108227f50>
#<type 'function'>
Enter fullscreen mode Exit fullscreen mode

The benefits of lambda

But, why we want a function without a name?

Because naming is too damn hard! Think about how much time you spent on naming(variables, functions, classes) when you are programming.

In fact, not all functions deserved a name.

Some functions are used temporarily and we don’t need them later. We use a lambda function to saving time for naming and get better readability.

Suppose we need to add 2 to each element in a list, instead of use normal function:

def add_2(x):
    return x + 2

lst = [3,5,-4,-1,0,-2,-6]
map(add_2, lst)
Enter fullscreen mode Exit fullscreen mode

We could use lambda to finish the same computation in one line:

map(lambda x: x +2, lst)
Enter fullscreen mode Exit fullscreen mode

This is simplicity. We can write a lambda function with no hassle.

There are other functions like filter, reduce, sorted, they receive lambda function as a parameters.

The pitfall of lambda

The purpose of lambda function is to improve the code’s readability. So if the logic of a function is not simple, we should not use lambda.

A simple rule is: don’t use lambda for the functions whose lengths are more than one lines.

Think about this code snippet, could you understand this code easily?

f = lambda x: [[y for j, y in enumerate(set(x)) if (i >> j) & 1] for i in range(2**len(set(x)))]
Enter fullscreen mode Exit fullscreen mode

Obviously, this code is difficult to understand. The intention of this code is to get all the subsets from a set.

a = {1, 2, 3}
print(f(a))
# [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
Enter fullscreen mode Exit fullscreen mode

In this case, we should use a normal function with a proper name:

def powerset(s):
    N = len(s)
    result = []
    for i in range(2 ** N):
        combo = []
        for j, y in enumerate(s):
            if (i >> j) % 2 == 1:
                combo.append(y)
        result.append(combo)
    return result

print(powerset(a))
Enter fullscreen mode Exit fullscreen mode

The post Python: Pros and Cons of Lambda appeared first on Coder's Cat.

Top comments (0)