Python three-amp generator

Source: Internet
Author: User
Tags for in range

In Python, the three-amp has iterators, generators, adorners, and this article mainly describes the generator. Mainly from the concept of generators, nature, and the use of the yield keyword to execute the process.

essence: A generator is a special kind of iterator, A function that uses the yield keyword is no longer a function, but a generator. (a function that uses yield is the generator)

The 1.yield keyword has a two-point effect:

The 1.1 yield statement returns one result at a time, saves the current running state (breakpoint), then pauses execution, suspends the state of the function so that the next time it is left, resumes execution, and the generator (function) hangs

1.2. Return the value of the expression after the yield keyword as the return value, which can be understood as the function of return

1.2.1 can use the next () function to let the generator continue execution from the breakpoint, which is the wake-up builder (function)

The generator in 1.2.2 Python3 can use return to return the final running return value, while the generator in Python2 does not allow return returns with return (that is, you can use return to exit from the generator, but you cannot have any expressions after return).

2. Advantages:

2.1 Use a generator function with less code.

The benefit of the 2.2 generator is the lazy calculation, which returns one result at a time and does not produce all the results at once, which is very useful for large data processing, which saves memory.

3. Build Method:

3.1 Generator expression

3.2 Generator function.

4. Code implementation:

4.1 Create Generator Method 1, replace the list-producing [] with (), the generator expression.

 for   in range (6)]in [2]: liout[2]: [0, 2, 4, 6, 8,]in [3]: type (LI) out[3]: Listin [ for in range (6)] in [5]: liout[5]: <generator object <genexpr > at 0x7fef38260780>inch [ for in li:   ...     :Print  (item)   ...:     0246810

4.2 Create Generator Method 2, using the yield keyword function.

deffib (num): I=0; NUM1, num2= 0, 1 whileI <Num:yieldnum1 num1, num2= num2, Num1 +num2 I+ = 1return "None"Gen_fib= FIB (3)#For item in GEN_FIB:#print (item)Print(Gen_fib.__next__())Print(Gen_fib.__next__())Print(Gen_fib.__next__())Try:    Print(Gen_fib.__next__())exceptstopiteration as E:Print(E.args)Print(E.value)#0#1#1#(' None ',)#None

4.3 Use the Send () wake function, you must first use Next (f), or f.__next__ () to start, before you can use the Send () function. __next__ (equivalent to none)

First, start with __next__, and when the Python interpreter encounters the yield keyword, stop execution, that is, the yield left assignment is no longer executed. When you call __next__ again, or send,

start execution yield left assignment operation. The value of temp is the parameter value of send, regardless of I, if you are using __next__ wakeup instead of send wakeup, then the value of temp is none.

defFun (): I=0 whileI < 5: Temp=yieldIPrint("Temp:", temp) I+ = 1F=Fun () F.__next__() F.__next__() f.send ("I'm send .")#Results#Temp:none#Temp: I'm send

Python three-amp generator

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.