Introduction to Python Anonymous Function

Source: Internet
Author: User
Keywords python anonymous function python anonymous function
definition
Anonymous function refers to a type of function or subroutine that does not need to define an identifier. Python uses lambda syntax to define anonymous functions. It only needs expressions without declaration.

The definition of lambda syntax is as follows:

# @param Quick introduction to anonymous Python functions
# @author Script House jb51.cc|jb51.cc
lambda [arg1 [,arg2,... argN]]: expression
# End www.jb51.cc

The syntax is the same as the definition of the following function except that there is no function name.

Features of anonymous functions
1. No need to name, because it is a headache to name a function, especially when there are many functions

2. Can be defined directly in the place of use, if you need to modify, you can directly find the modification, which is convenient for future code maintenance

3. Simple syntax structure, no need to use def function name (parameter name): defined in this way, directly use lambda parameter: return value can be defined

The name lambda comes from LISP, and LISP takes the name from lambda calculus (a form of symbolic logic). In Python, lambda is used as a keyword, as the syntax for introducing expressions. To compare def functions, lambda is a single expression, not a statement block! You can only encapsulate limited business logic in lambda. The purpose of this design is to make lambda designed purely for writing simple functions, and def is focused on processing Bigger business.

Difference from ordinary functions
Common functions:

# @param Quick introduction to anonymous Python functions
# @author Script House jb51.cc|jb51.cc
#Ordinary function with two parameters
def add(x,y):
    return x*y
rs = add(10,20)
print(rs)
# End www.jb51.cc

Anonymous function:

# @param Quick introduction to anonymous Python functions
# @author Script House jb51.cc|jb51.cc
#Anonymous function with two parameters
b = lambda x,y:x*y
rs = b(10,20)
print(rs)
# End www.jb51.cc

Examples
# @param Quick introduction to anonymous Python functions
# @author Script House jb51.cc|jb51.cc
#Calculate the square of each number in the list of 100 numbers and return a new list
print(list(map(lambda x:x*x,[x for x in range(100)])))
# End www.jb51.cc
 


Results:

# @param Quick introduction to anonymous Python functions
# @author Script House jb51.cc|jb51.cc
[0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,1024,1089,1156,1225,1296,1369,1444,1521,1600,1681,1764,849 ,2209,2304,2401,2500,2601,2704,2809,2916,3025,3136,3249,3364,3481,3600,3721,3844,3969,4096,4225,4356,4489,4624,4761,4900,5041 ,5184,5329,5476,5625,5776,5929,6084,6241,6400,6561,6724,6889,7056,7225,7396,7569,7744,7921,8100,8281,8464,8649,8836,9025,9216 ,9409,9604,9801]
# End www.jb51.cc
Related Article

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.