Python Generator (yield)

Source: Internet
Author: User

For calling a normal Python function, it is generally executed from the first line of the function, ending with a return statement, an exception, or all the statements of the function. Once the function returns control to the caller, it means that it is all over. All the work done in the function and the data saved in the local variables will be lost. When you call this function again, everything will be created from scratch. Python is a builder that implements a concept similar to a synergistic program: The generator can suspend functions temporarily, retain data such as local variables of the function, and then continue execution from the last paused position when it is called again.

Improve your Python: Explain yield and generators (generator)

>>> a = [i forIinchRange (100)if  not(i%2) andi%3]>>>a[2, 4, 8, 10, 14, 16, 20, 22, 26, 28, 32, 34, 38, 40, 44, 46, 50, 52, 56, 58, 62, 64, 68, 70, 74, 76, 80, 82, 86, 88, 92, 9 4, 98]>>> b = {i:i% 2 = = 0 forIinchRange (10)}>>>B{0:true,1:false, 2:true, 3:false, 4:true, 5:false, 6:true, 7:false, 8:true, 9: False}>>> C = {i forIinch[2,1,1,1,5,6,9,0,3,4,5,5,7,6,8,3]}>>>c{0,1, 2, 3, 4, 5, 6, 7, 8, 9}>>> d ="i-I in ' I love fishc.com '">>>D"i-I in ' I love fishc.com '">>> E = (i forIinchRange (10))>>>e<generator Object <genexpr> at 0x0000000002f08af8>>>>Next (E) 0>>>Next (E)1>>>Next (E)2>>>Next (E)3>>> foreachinche:Print(each)456789>>> SUM (i forIinchRange (100))4950

The above is a list deduction, in fact, there is a dictionary derivation, and no tuple derivation, the result of the tuple derivation is actually a generator, as the above example of E.

def fibs ():     = 0    = 1     while True:        = B, A + b        yield  a          for in fibs ():    if all >:        break     Print(each, end=")    

The code above is the Fibonacci sequence implemented by the generator. When the generator encounters the yield keyword, the function can be temporarily suspended, and the function will continue to execute from the last place of the generator only when the generator is re-accessed.

Python generator (yield)

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.