Today, I learned about the use of yield when learning to build a crawler scrapy frame. Understand a thing, no what? How? Why?
What
Yield in English means surrender, abdication, give up. The amount ... It's actually a keyword in python. In layman's terms, you'll turn yellow when you play in idle. (>) <)
How
Apart, let me give you an example.
def test_yield(n): for i in range(n): yield i**2for i in test_yield(5): print(i, " ",end="")
Output what? Try it yourself, let's talk first.
You can't sit down, what's the difference between this and return????
and write a return below.
def sqare(n): ls = [i**2 for i in range(n)] return lsfor i in sqare(5): print(i, " ",end="")
Output what? Still use to say >)
Yes, that's the way I met the yield, which seems to be the same as return.
High Energy aheadFALSE why
Since it is similar to return usage, why yield????
Then you can try this.
def sqare(n): ls = [i**2 for i in range(n)] return lsfor i in sqare(1000000000000000000000000000000): print(i, " ",end="")
Silly face of it:)
You try this again.
def yield_test(n): for i in range(n): yield i**2for i in yield_test(1000000000000000000000000000000): print(i, " ",end="")
Does it feel good to have to ha haha
Principle to say, or to say, to avoid me later forget. : )
TRUE Whyadvantage 1
Yield keyword It can freeze the loop after the loop executes once, freezes!!! Don't wait to start again!!!
So when you call it with a for loop, it can be output over and over again, not as a return to a one-time output, greatly enhancing the program's observability.
Advantage 2
Yield of the output, so that it outputs the result of only one element of storage space
Return is a brother.
There's really nothing to compare
When appropriate, the use of appropriate keywords, all of them, constitutes the most great program
Yield deep
The yield keyword uses a lot of places, scrapy crawler frame What, continue to learn.
If life is just like the beginning, what autumn wind sad painting fan.
Python life is like the beginning of the initial yield