Deep understanding of Python Builder expressions and list parsing __python

Source: Internet
Author: User
Tags generator
Preface

There is no use of things, there is no deep understanding of the things you will be, and be asked by others necessarily flawed. Although there were previous concepts of touching Python coprocessor, but just a cursory, the two days of a conversation, others asked the tongue-tied, immediately, anyway, can not think of what has been seen, and then suddenly thought of yield, but it is too late, can only say that the concept is not clear, so the first strands of this article Python's generator and yield keywords . what is a generator builder is a special program that can be used as an iterative behavior generator for the control loop is similar to a function that returns an array of values that can receive parameters that can be invoked, but Unlike normal functions, which return an array containing all the values at once, the generator produces only one value at a time , the amount of memory consumed is greatly reduced, and allowing the calling function to quickly start processing the first few return values. Therefore, the generator looks like a function but behaves like an iterator. generators in Python

Python provides two basic ways to do this. Generator functions: also defined by Def, using the keyword yield to return one result at a time, blocking, restarting the builder expression: Returns an object that produces results only when needed


Here is a detailed explanation:


Generator Functions

Why call the generator function. because he generated a numeric queue over time . The normal function returns a value after execution and exits, but the generator function hangs automatically, then picks up to continue execution, and he uses the yield keyword to close the function, return a value to the caller, and retain enough current state to allow the function to continue executing. The generator and the iteration protocol are closely related , and an iterative object has a __next () __ Member method that either returns the next item in the iteration or causes an exception to end the iteration.
To support the iterative protocol, functions that have yield statements are compiled into generators, and such functions are invoked to return a generator object, and the returned object supports an iterative interface, that is, the member Method __next () continues execution from the interrupt.

Look at the following example:

# Counter Def create_counter (n): Print ("Create counter") while True:yield n print (' increment n ') n = 1 cnt = Create_counter ( 2) print (CNT) print (next CNT) print (next CNT) print (next cnt) print (D:\Python\python\python-3.6.1\) # Python36-64\python.exe # <generator Object create_counter at 0x0000000002271d00> # create counter # 2 # increment N # 3 # Increment N # 4 # increment N # 5


To analyze This example: A keyword yield appears in the Create_counter function, indicating that the function produces only one result at a time, which returns a generator (as can be seen from the first row of output) to produce a continuous n value when creating a generator instance. It just needs to be called like a normal function, but the call does not execute this function, which can be seen through the output the next () function takes the generator object as its own argument, and at the first call he executes the Create_counter () function to the yield statement, Return the resulting value 2 we repeated the call to the next () function, and each time he would start execution from the last place he was suspended until he met the yield keyword again

For a more profound understanding, let us cite another example.

# cube def cube (n): For I in range (n): Yield i * * 3 print (cube (5)) print (Next (Cube (5))) print (Next (Cube (5))) Print (cube (5)) print ("For Loop") for the I in Cube (5): Print (i) # D:\Python\python\python-3.6.1\Python36-64\python.exe # <generator OB Ject Cube at 0x0000000001fa1d00> # 0 # 0 # 0 # for Loop # 0 # 1 # 8 # 27 # 64

So from the perspective of the function we can compare the yield to return, but the function is really different, in the For loop, the iteration rules are automatically followed, each time the next () function is called, so the result is not difficult to understand. Builder expression:

The builder expression comes from a combination of iteration and list resolution , and the builder expression is similar to the list resolution, but he uses parentheses instead of square brackets. The following code:

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.