So, in MicroPython, this means that we can define functions at run-time without the typical def structure. Although they have many uses, one particular application of lambdas is their use in functions such as the map, filter and reduce, which are applied to lists.
The tests shown here were performed on both the ESP32 and the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The images shown through the tutorial are from the tests on the ESP32.
The commands shown bellow were executed on uPyCraft, a MicroPython IDE. You can learn more about it in this previous post.
If you prefer, you can also check a video version of the tutorial bellow, at my Youtube channel.
In order to test lambdas, we are going to do a simple example where we will both use a lambda and a named function to achieve the same result of applying an operation over two variables.
So, we will first define a function that receives as input two values and sums them. We will use the def keyword, so it will be a named function, that we can later use in our code.
Next we will define a lambda function. To do so, we need to use the lambda keyword and the corresponding syntax, which is shown bellow.
lambda args: expression
So, after the lambda keyword we specify the arguments and then after “:” we put the expression for our function.
In case we have multiple arguments, we need to separate them using commas. Nevertheless, we should not enclose them in parenthesis, which will throw an invalid syntax error in MicroPython.
One important thing to note is that in the expression part of the lambda we don’t use a return statement as we would in a regular function. This is because lambdas can only contain expressions and return is a statement . You can read more about the difference of an expression and a statement in Python here.
As said in the previous section, in our simple example we will create a regular function and a lambda to sum two numbers. We can define the function with the code shown bellow. It will simply receive two input parameters and return their sum.
After the definition of the function, we can later call it in our code, as we are also doing bellow.
def sum(x,y): return (x+y) result = sum (4,5) print(result)
You can check bellow at figure 1 the expected result of defining and executing our sum function.
Now we are going to define a lambda function, accordingly to the syntax we introduced in the previous section. As mentioned before, the multiple parameters of the function (x and y) should be separated by commas but not be enclosed in parenthesis.
Then, after the “:”, we write the expression that sums both of the arguments. Nonetheless, no return statement is used, which respects the lambdas requirements.
sumLambda = lambda x,y: x+y result = sumLambda(4,5) print(result)
Note that we assigned the lambda to a variable so we could later apply it to two values. Nonetheless this is different from the previous case because if we loose that lambda variable (for example, we assign it other value), our lambda will be lost, since it is not a named function. Besides that, in many practical use cases, we don’t even assign it to any variable since we simply use it as input of functions, such as the previously mentioned map, reduce and filter.
You can check the expected result bellow at figure 2, which gives the same result of executing the named function.
In this tutorial, we saw how to create and execute a lambda function. Note that lambdas are a more advanced feature of Python / MicroPython, so it is normal that they don’t appear so often in introductory tutorials.
Although we can easily program without using lambdas, they offer a new level of flexibility and it is a concept worth learning about. Although this introductory tutorial shows a simple example that doesn’t expose how useful they are, they become a very practical tool if you spend much time writing map / reduce functions, for example.
Personally, I’ve started using lambdas while working with C# and its awesome LINQ syntax. At first, using lambdas felt complex and unnecessary, but with time it makes it easier to write code more compact and elegant. But be also careful since it will make your code harder to read for people who don’t know the lambdas syntax.