The python generator has simple code to understand.

Source: Internet
Author: User
__author__ = ' zhht ' #!/usr/bin/env python#-*-coding:utf-8-*-#返回当前执行到的函数的返回值. and maintains the status of the current execution. It's time to do something else. The next time you execute, the location where the last call was received continues to execute the code. def fib (num):    n = 0    a,b=0,1 while    n <num:        yield b        a,b=b,a+b        n+=1    return ' complete ' F = fib (5) While True:    try:        x = Next (f)        print ("value%d"%x)    except Stopiteration as E:        print ("Generator return value-'%s %e.value)        break# The second is an example: Def gen ():    i = 0 while    i < 5:        temp = yield i        print (temp)        i+= 1f = Gen () print (Next (f)) print (Next (f)) print (next) #它和上面的相同点就是能够继续迭代执行. The point is that the process goes to yield returns the value of I, and at the end of the execution, temp receives none, while send sends a value of your temp acceptance in such a case print (F.send ("HAHA1")) Print (F.send (" HAHA2 "))

  

The python generator has simple code to understand.

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.