Python's Lambda anonymous function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.