Python high-order functions

Source: Internet
Author: User

First, generator

Reason for using the generator: The list-generated element consumes more memory when it is printed, and the generator generates only one value per call, does not consume too much memory, and the generator is called lazy evaluation, deferred computation.

B read with A For loop (reason: The generator is an iterative object)

Generator Simple Instance
Using generators to get Fibonacci sequences

3 Yield: the ability to generate keywords for generator objects

To make the next () pointer stop before yield

The builder model for buying books

No-buffer customization and purchase of book models

If there are intermediate and buffer, you need to define a list and add it to the list

Builder Practical application: Mini chat Robot

Second, built-in high-order functions

1 map: Each element of the given sequence (s) of the function Func scope, and provides a return value with a list, if Func is None, Func behaves as an identity function that returns a list of n tuples containing the set of elements in each sequence.

Its function passes in a value of one and cannot pass in more than one value at a time

2 reduce: The elements of the two-dollar function with the SEQ sequence, each carrying a pair (the previous result and the next sequence element) successively the existing results and the next value on the obtained results, and finally reducing our sequence to a single return value, if the initial value of the Init is given, The first comparison will be init and the first sequence element instead of the sequence's first two elements

Only two of its function arguments can be passed in, but not multiple parameters at the same time

3 Filter: Invokes a Boolean function Func to iterate through the elements in each SEQ, returning a sequence of elements that enable Func to return a value of true

4 sorted: For sorting

Default Reverse=false, which are arranged in order from small to large, if reverse=true indicates that they are arranged in order from large to small

instance: Sorting elements in a list without case sensitivity

Third, lambda function (anonymous function)

Lambda function name: function return value
LAMDBA operator

LAMDBA args:expression (call and process parameters)
Args: comma-separated list of parameters
Expression: Expressions that use arguments in args

The code defined by the LAMDBA statement must be a valid expression, not a multi-conditional statement (a ternary expression that can use if), and other non-expression statements, such as for and while, etc.

The lambda function is used to calculate the square of a sequence and return a sequence

The lambda function combined with the reduce higher order function to sort the specified elements in a multiple list

Closed Package

The main motive behind the adorner is the Python object-oriented programming, which is a decoration on the function call, which is applied only when a function or method is declared.
The adorner's syntax begins with the @, followed by the adorner function's name and optional parameters, followed by the decorator's declared function, and optional parameters for the adornment function.

1 form of the basic adorner

2 use of adorners that pass in default parameters and must parameters in a function

3 The use of the adorner to import the module time to perform the calculation function

4 use of adorners that pass in variable parameters and keyword parameters in a function

5 handling of the incoming value of the adorner

6 adorners are created in adorners that pass in multiple different types of values

Five Practical examples:

Python high-order functions

Related Keywords:

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.