Default arguments in lambda expressions. Lambda expressions in the body of an enclosing function. Nested lambda expressions
Contents
- 1. Using default arguments in lambda expressions. Example
- 2. Examples of lambda expressions containing default arguments
- 3. Using lambda expressions within an enclosing function. Example
- 4. Nested lambda expressions. Example
- Related topics
Search other websites:
1. Using default arguments in lambda expressions. Example
Like functions, lambda expressions can contain default arguments. The general form of using the default argument is:
lambda arg1=value1, arg2=value2, ..., argN=valueN : expression
here
- arg1, arg2, argN – the names of the arguments that the lambda expression uses;
- value1, value2, valueN – values that are assigned respectively to the arguments arg1, arg2, argN;
- expression – the lambda expression itself.
If you do not specify the values of the arguments when calling the lambda expression, then they will be assigned the default values value1, …, valueN. If you specify only part of the arguments, then this part will replace the default value starting from the leftmost argument (arg1).
If you declare a lambda expression that takes 3 arguments by default, and call the lambda expression with 1 argument, then the changes will affect the leftmost argument. The following example demonstrates the rule for overriding default arguments when invoking a lambda expression.
# Using default arguments in lambda expressions # Lambda expression that takes 3 arguments by default summ = lambda a=1, b=2, c=3: a+b+c print("summ() = ", summ()) # 1+2+3 = 6 print("summ(10) = ", summ(10)) # 10+2+3 = 15 print("summ(10, 20) = ", summ(10,20)) # 10+20+3 = 33 print("summ(10, 20, 30) = ", summ(10,20,30)) # 10+20+30 = 60
⇑
2. Examples of lambda expressions containing default arguments
Example 1. An example of a lambda expression that returns the area of a circle at a given radius. By default, the lambda expression sets the radius of the circle to 1.
# Using default arguments in lambda expressions import math # Lambda expression that returns the area of a circle areaCircle = (lambda r = 1: math.pi*r*r) # Calculate the area of a circle of radius 5 area5 = areaCircle(5) print("area5 = ", area5) # Calculate the area of a circle of radius 1 - default area1 = areaCircle() print("area1 = ", area1)
The result of the program
area5 = 78.53981633974483 area1 = 3.141592653589793
Example 2. Calculation of the distance from the point with coordinates (x; y) to the origin. The point coordinate is set to x = 1, y = 1 by default.
# Using default arguments in lambda expressions import math # Lambda expression that returns the distance from a point to the origin lengthOrigin = (lambda x=1, y=1: math.sqrt(x*x+y*y)) # Calculate distance from point (-5; 2) to origin L = lengthOrigin(-5,2) print("L(5;2) = ", L) # Calculate default point distance (1; 1) L = lengthOrigin(1,1) print("L(1;1) = ", L)
Program result
L(5;2) = 5.385164807134504 L(1;1) = 1.4142135623730951
⇑
3. Using lambda expressions within an enclosing function. Example
From the point of view of searching for variables in scopes, the following rule can be distinguished for lambda expressions:
- if a lambda expression is declared inside a function, then that function is considered to be enclosing for the given lambda expression. Accordingly, this lambda expression has access to the enclosing function names (see example 1 below).
Example. This example demonstrates the declaration of a lambda expression inside a function. The GetRandomNumber() function is declared, which returns an integer in the range from 0 to 99. In the body of the function, a real random number is first formed in the range [0..1]. Then, a lambda expression is declared that converts this number to an integer with a value in the range [0..99]. As you can see from the code below, the lambda expression has access to the variable number, which is declared in the code of the enclosing GetRandomNumber() function.
# Using lambda expressions within an enclosing function import random # A function that returns an integer in the range 0 to 99 def GetRandomNumber(): # 1. Get a random number in the range 0..1 number = random.random() # 2. Declare a lambda expression # that converts a real number in the range [0..1] # to an integer in the range [0..99] number99 = lambda : int(number*100) # access to number # 3. Return an integer return number99() num = GetRandomNumber() print("num = ", num)
The result of the program
num = 51
⇑
4. Nested lambda expressions. Example
As you know, a lambda expression is declared on one line. This simplifies somewhat the logic of the code that can be used in a lambda expression. However, in Python lambda expression can be nested. With the help of nested lambda expressions, you can slightly diversify (complicate) the computational code.
The general form of a nested lambda expression looks like this
lambda arg11, ..., arg1N: (lambda arg21, ..., arg2N : expression)
here
- arg11, arg1N – arguments of the external lambda expression;
- arg21, arg2N – arguments of the nested lambda expression.
Several levels of nesting are allowed.
Example. The example demonstrates how to calculate the sum of three numbers using nested lambda expressions.
# Nested lambda expressions # Calculate the sum of two numbers sum2 = (lambda a: (lambda b: a+b)) s = sum2(5)(4) print("sum2(5)(4) = ", s) # Calculate the sum of three numbers sum3 = (lambda a: (lambda b: (lambda c: a+b+c))) print("sum3(2)(4)(6) = ", sum3(2)(4)(6))
Program result
sum2(5)(4) = 9 sum3(2)(4)(6) = 12
Как видно из вышеприведенного кода, в строках
sum2 = (lambda a: (lambda b: a+b))
and
sum3 = (lambda a: (lambda b: (lambda c: a+b+c)))
another function is created inside the lambda expression. In all cases, lower-level lambda expressions have access to the variables of the enclosing lambda expressions (functions).
⇑
Related topics
- Lambda expressions. Anonymous functions. The lambda keyword. Transition table
- Implementing complex logic in lambda-expressions. Condition check. Using lambda-expressions for sequences. Functions map(), filter(), reduce()
⇑