In Python, Lambda allows the user to quickly define a single-line function , and the user can, of course, perform the function according to a typical function definition. The purpose of lambda is to simplify the process of user-defined usage of functions.
>>>Func=lambda X: x * 2 # define a lambda function that is called by assigning a value to func >>> Func (3 6 >>> (lambda X : x *2 (3 ) # the definition and invocation of a lambda function can be integrated (poor readability) 6
Some points to note when using lambda functions:
- Lambda defines a single-line function, and if a complex function is required, a normal function should be defined
- A lambda parameter list can contain multiple parameters, such as Lambda X, y:x + y
- Expressions in lambda cannot contain commands, and only one expression
In addition one detail is added:
We know that using a global variable in a function is simply adding a "global var" statement to the function, and in fact there is a detail that the variable in the Python default function is a global variable, and once it is assigned a value at some point in the function body, it is considered a local variable.
In Python, variables is only referenced inside a function is implicitly global. IF A variable is assigned a new value anywhere within the function ' s body, it's assumed to be a local.
#!/usr/bin/python var= def func_local():printvar# var is a global variable. Also here just use the value of Var without changing it, if you want to change the global variable, you should add "global var" def func_global():var=# var is a local variable
Python's Lambda anonymous function