Python life is like the beginning of the initial yield

Source: Internet
Author: User

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

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.