"Python learning" yield send I said so much.

Source: Internet
Author: User

C # 's yield has been forgotten almost. Also encountered the yield of Python.
Iterator
defTestyield ():Print 'Yield1'm=yield1Print 'm =', MPrint 'yield2' yield5 forAinchTestyield ():Print 'Test'result=Testyield () result.send ('Test')PrintList (Result)

OUTPUT:

Yield1
Test
m = None
Yield2
Test

File "C:\pytest\Sele\Generator.py", line $, in <module>
Result.send (' Test ')
Typeerror:can ' t send non-none value to a just-started generator
[Finished in 0.2s with exit code 1]

Refer to this inside said:

Http://www.jb51.net/article/15717.htm

Send (something) and next ()

The first call, send cannot pass non-null value, do not know why, later understand

Yield appears in the function, indicating that the function is generator, and the generator is treated differently.

Give me a chestnut ~

def Testyield ():         yield 1    print  type (m)    print  m    yield 5     yield= Testyield ()printPrint Result.send ('fighting')
Result.next ()
Print Result.next ()
# Print List (result)

The Da Da, into the function factory
Computer: OK, it's done. Here you are. 1

Print.send (' Fighting ') B: Make a piece of cake for me, let ' fighting ' do it, and pack it up
The Da Da, into the function factory
Computer: OK, let fighting do it. Here you are. 5

The Da Da, into the function factory
Computer: OK, it's done. Give you 88, eh? What about people?

The Da Da, into the function factory
Computer: No raw materials, do not come out of the cake, stop selling stop iteration
Print List (result) E: see what you can do.
The Da Da, into the function factory
Computer: We have empty output ""

The output of the serious is:

1
<type ' str ' >
Fighting
5

File "C:\pytest\Sele\Generator.py", line-up, in <module>
Print Result.next ()
Stopiteration

result = Testyield ()

Result.next () #启动了这个生成器 until the first yield statement hold

You go to fetch this result.next (), such as print result.next (), this value is actually generated, the type of Result.next () is int

Result.send (something), this something will be passed to the yield expression, m= ' something '. Type (m) is str. print m to get the value you passed in with send.

What does it mean?

You use Send to pass something, if you do not use in the method, it is useless. I think so for the time being.

Result.send (something) continues to look for the next yield, and hold

Like Next (), send also causes the generator to stop there.

Print Result.send (something) is not something. Print is the value of the next yield found by generator.

The generator of this translation is the return value of the generated function. What do you mean, yield English? Produce produces a thing

Look at one more chestnut ~

defh ():Print 'Wen Chuan'm=yield5#fighting!    Print 'm=', M D=yield12Print 'We are together!'    PrintD Test=yield35PrintTESTC=h () current= C.next ()#start generator, stop at the first yieldPrintcurrentcurrent= C.send ('fighting!')#send current yield ' fighting ', stop at the next yieldPrintCurrentc.send ('got it')Printlist (c)PrintList (h ())

Output:

Wen Chuan
5
M= fighting!
12
We are together!
Got it
None
[]
Wen Chuan
m= None Next () = Send (None)
We are together!
None
None
[5, 12, 35]

"After a year, I learned that Python was almost forgotten. So I'm going to re-learn.

Http://anandology.com/python-practice-book/iterators.html

Say something normal people can read. I didn't understand what I had written before. The following English is excerpt.

Generators simplifies creation of iterators. A generator is a function this produces a sequence of results instead of a single value.

Each of the yield statement is executed the function generates a new value.

So a generator are also an iterator. You don ' t has to worry about the iterator protocol.

The word "generator" is confusingly used to mean both the function, the generates and what it generates. In this chapter, I'll use the word "generator" to mean the Genearted object and "generator function" to mean the function that generates it.

Can you think what's it is working internally?

When a generator function is called, it returns a generator object without even beginning execution of the function. When next method was called for the first time, the function starts executing until it reaches yield statement. The yielded value is returned by the call next .

The following example demonstrates the interplay between and call to yield next method on generator object.

Well, that's a good word, I'll translate it.

Say, this generators is a simplification of iterator's creation. A generator is a method used to generate a string of results, rather than just returning a value.

Each time the yield statement is executed, the method produces a new value.

So, this generator is also an iterator. You don't have to worry about the rules of iterator. Whether it applies or not, the answer is yes.

Does the word "generator" mean what it does to generate or to say what it generates? This is confusing. In our case, I use the generator to represent the generated object, and use the generator function to represent the function of what it generates.

When a generator function is called, it returns a generator object, just returning an object, not doing anything else, or even starting to execute the method.

When the next method is called for the first time, the method starts executing until it encounters a yield statement. This yield (mass production) value is the return value of next.

"Python learning" yield send I said so much.

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.