Python System Study Notes (13th) --- lambda

Source: Internet
Author: User

1. python lambda creates a function object, but does not assign this function object to an identifier. def assigns the function object to a variable. 2. python lambda is just an expression, while def is a statement. Lambda statements are used to create new function objects and return them at runtime. #! /Usr/bin/python # Filename: lambda. pydef make_repeater (n): return lambda s: s * ntwice = make_repeater (2) print twice ('word') print twice (5) Here, we use the make_repeater function to create a new function object at runtime and return it. Lambda statements are used to create function objects. Essentially, lambda requires a parameter, followed by a single expression as the function body, and the value of the expression is returned by the newly created function. Note that even print statements cannot be used in the lambda format, but expressions are only allowed. 2 for multiple parameters: m = lambda x, y, z: (x-y) * zprint m (3,1, 2) the result is that the 4 job will join the blood discharge in the next chapter list. Here we will briefly introduce the benefits of the main application. The functions www.2cto.com that are not reused can be replaced by this, and of course they are magical and concise! !!! Print reduce (lambda x, y: x * y, range (1, 1001) reduce and range are both Python built-in functions. Range (1000) indicates generating a List of consecutive integers from 1 ). Reduce (functionA, iterableB), functionA is a function that requires two variables and returns a value. IterableB is an iterative variable, such as List. The reduce function transmits the elements in B from left to right to function A, and then replaces the input parameters with the results returned by function A. After Repeated execution, B can be reduced to A single value. Here, it is to pass the continuous integer list from 1 to 1000 into the lambda function and replace the number in the list with the product of two numbers. The actual calculation process is :(... (1 × 2) × 3) × 4) ×... * 1000). The final result is the factorial of 1000.

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.