Master Python Lambda Functions With These 4 Don’ts
Use lambdas, but don’t misuse them
Lambda functions are anonymous functions in Python. Using them is a handy technique in a local environment when you need to perform a small job. Some people simply refer to them as lambdas, and they have the following syntax:
lambda arguments: expression
The creation of a lambda function is signaled by the lambda
keyword, followed by the list of arguments and a single expression separated by a colon. For instance, lambda x: 2 * x
simply multiplies any input number by two, while lambda x, y: x+y
simply calculates the sum of two numbers. The syntax is pretty straightforward, right?
With the assumption that you know what a lambda function is, this article is intended to provide some general guidelines on how to use lambda functions properly.
1. Don’t Return Any Value
Looking at the syntax, you may notice that we don’t return anything for the lambda function. It’s all because lambda functions can only contain a single expression. However, the use of the return
keyword will constitute a statement that is incompatible with the required syntax, as shown below:
This mistake probably arises due to the inability to differentiate expressions from statements. Statements like those involving return
, try
, with
, and if
perform particular actions. However, expressions are those that can be evaluated to a single value, such as a number or other Python objects.
With lambda functions, the single expression will evaluate a single value that is used subsequently, such as being sorted by the sorted
function.
2. Don’t Forget About Better Alternatives
One of the most common use cases is to set a lambda function to the key
argument of some built-in utility functions, such as sorted()
and max()
, as shown above. Depending on the situation, we can use other alternatives. Consider the following examples:
In data science, many people use the pandas library to process data. We can use the lambda function to create new data from existing data using the map()
function, as shown below. Instead of using a lambda function, we can simply use the arithmetic function directly because it’s supported in pandas:
3. Don’t Assign It to a Variable
I’ve seen some people mistakenly think that a lambda function is an alternative way to declare a simple function, and you may have seen people do the following:
The only use of naming a lambda function is probably for teaching purposes to show that a lambda function is indeed a function just like other functions — to be called and having a type of function. Other than that, we shouldn’t assign a lambda function to a variable.
The problem with naming a lambda function is that it makes debugging less straightforward. Unlike other functions that are created using the regular def
keyword, lambda functions don’t have names, which is why they’re sometimes referred to as anonymous functions. Consider the following trivial example to see this nuance:
- When your code has problems with a lambda function (i.e.
inversive0
), theTraceback
error information can only tell you that a lambda function has bugs. - By contrast, with a regularly defined function, the
Traceback
will clearly inform you of the problematic function (i.e.inversive1
).
Related to this, if you have the temptation to use a lambda function more than once, the best practice is to use a regular function using the def
keyword, which will also allow you to have docstrings.
4. Don’t Forget About List Comprehension
Some people like to use lambda functions with higher-order functions, such as map
or filter
. Consider the following example for this usage:
Instead of using the lambda function, we can use list comprehension, which has better readability. As shown below, we use list comprehension to create the same list objects. As you can see, the previous usage of map
and filter
functions with lambda functions is more cumbersome compared to list comprehension. So you should consider using list comprehension when you’re creating lists involving higher-order functions.
Conclusion
In this article, we reviewed four common mistakes that someone may make with lambda functions. By avoiding these mistakes, you should be able to use lambda functions properly in your code.
The rule of thumb for using lambda functions is to keep it simple and use them just once locally.
'Data Analytics(en)' 카테고리의 다른 글
10 Crazy Cool Project Ideas for Python Developers (1) | 2020.10.29 |
---|---|
7 Awesome Command-Line Tools (0) | 2020.10.28 |
Change The Way You Write Python Code With One Extra Character (0) | 2020.10.26 |
Data-Preprocessing with Python (0) | 2020.10.25 |
Advanced Python: 9 Best Practices to Apply When You Define Classes (0) | 2020.10.24 |