Yield expression python syntax

Source: Internet
Author: User

You can read this article first: http://www.cnblogs.com/jiangtu/articles/6662043.html

The original article is reproduced: http://www.python-tab.com/html/2015/pythonhexinbiancheng_0415/946.html (Remove the hyphen-, the blog Park display prohibited words. )

The yield expression was not known before, only the function that contains the yield expression is compiled into an iterator, such as the following code:

def g (N):      for inch range (N):         yield i **2#  print output  for in G (5):    Print I # output 0, 1, 4, 9, 16

The function g (n) contains the yield expression, so g (n) is compiled into an iterator.

Insert: The range is left open right close similar to: [...] (Strengthen memory--.)

But then, what does it mean to have multiple yield expressions in a function, or a return statement before the yield expression, or a yiled return statement? (In fact, there are more than one yield expression in g (n), but I didn't think of it at the beginning. )

The following code can be used to understand some of its processes:

#Coding:utf-8defg ():Print "before the first yield"    yield "yield for the first time"    Print "before the second yield"    yield "yield for the second time"    Print "end of function, no yield after"

The output is:

1>>>ImportTest2>>> fun =test.g ()3>>> Fun4<generator Object G at 0x7f43f6c4cf50>5>>>Fun.next ()6 before the first yield7 '\xe7\xac\xac\xe4\xb8\x80\xe6\xac\xa1yield'8>>>Fun.next ()9 before the second yieldTen '\xe7\xac\xac\xe4\xba\x8c\xe6\xac\xa1yield' One>>>Fun.next () A end of function, no yield after - Traceback (most recent): -File"<stdin>", Line 1,inch<module> theStopiteration

Can be seen,

    • function is actually compiled into an iterator (line-A-line),
    • When the next element of the iterator is returned, Fun.next (),
      • Executes the iterator's current position to the middle of the next yield code fragment (LINE5~7),
      • The return value is the value of the next yield expression (line 7).

In summary, the function that contains the yield expression is compiled into an iterator return, and when this iterator is iterated, the code between the yield expression is executed and the value after the yield expression is returned as the return value. First Next is the start of the function to a yield expression, and if there is no yield expression at the end, the last iteration throws an exception (Stopiteration). (For loop detects exceptions and automatically calls next ())

As for yield return, direct error. But how do I remember where I saw it?

Second, a specific MSG value can be passed to the yield expression via Fun.send (msg).

That is: fun.next () = = Fun.send (None) in detail can be seen in the beginning of the article reproduced

Finally, the yield expression is implemented to a certain extent, see connection: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000 /0013868328689835ecd883d910145dfa8227b539725e5ed000

Yield expression python syntax

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.