Chapter 3 python3, python3

Source: Internet
Author: User

Chapter 3 python3, python3

An anonymous function is a type of function or subroutine that does not need to define identifiers. Python uses lambda syntax to define anonymous functions. You only need to use expressions instead of declarations.
Lambda syntax is defined as follows:

lambda [arg1 [,arg2, ... argN]] : expression

Sometimes, when passing in a function, you do not need to explicitly define the function. It is more convenient to directly pass in an anonymous function. There is a limit on anonymous functions, that is, there can be only one expression, and no return is required. The return value is the result of this expression. There is a benefit to using an anonymous function because the function has no name and you don't have to worry about function name conflicts. In addition, an anonymous function is also a function object. You can assign an anonymous function to a variable and use the variable to call the function.

Instance:

# Writable function description sum = lambda arg1, arg2: arg1 + arg2 # Call the sum function print ("the sum value is:", sum (10, 20 )) print ("the sum value is:", sum (20, 20 ))

The above code outputs:

The value after addition is: 30. The value after addition is: 40.

 

It is called directly during definition, for example:

print((lambda x, y: x-y)(3, 4))

The above code outputs:

-1

 

However, when defining a lambda function, you usually pass its application as a parameter to another function. This function calls a lambda-Defined Function during its processing. For example:

L = [1, 2, 3, 4, 5, 6, 7, 8] # filter out all even res = filter (lambda x: x % 2 = 1, l) print (list (res ))

The above code outputs:

[1, 3, 5, 7]

 

 

The above examples are relatively simple. Let's look at some complicated ones. If we want to find two small and medium-sized ones, we can use lambda to write:

fn = lambda x, y: x if x < y else yprint(fn(1, 2))

The above code outputs:

1

 

In the above example, the writing method in the function body is actually a ternary expression (three-object operator) in python ). In Python, the syntax is as follows:

True result if the condition else is false

For example:

1 if 5>3 else 0

Is to output the result first, then determine the condition, output 1, if 5 is greater than 3, otherwise output 0

 

A few more complex ones. If you are a beginner, you can skip them!

Colihua (what is colihua? Https://baike.baidu.com/item/%E6%9F%AF%E9%87%8C%E5%8C%96/10350525? Fr = aladdin ):

res = (lambda x: (lambda y: (lambda z: x + y + z)(1))(2))(3)print(res)

The above code outputs:

6

 

Recursion:

func = lambda n: 1 if n == 0 else n * func(n-1)print(func(5))

The above code outputs:

120

Or

f = lambda func, n: 1 if n == 0 else n * func(func, n - 1)print(f(f, 4))

The above code outputs:

24

 

Anonymous functions have the following features:

  • Lambda is just an expression, and the function is much simpler than def.
  • The body of lambda is an expression rather than a code block. Only limited logic can be encapsulated in lambda expressions.
  • Lambda functions have their own namespaces, such as nested function definitions. lambda functions can reference variables from their scopes.

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.