A few questions about Python builder

Source: Internet
Author: User

1. Can the generator function take parameters?

Yes, the generator function supports all parameter forms, because the generator letter is one of the functions

Der Count (start=0): While True:yield startstart++

#指定从0开始的计数器



2. Can the generator function return a value with return output?

The answer is no, the generator's default return value is the generator, then you have to give him a return value this is not possible, in short, will be an error, do not believe you try.

In this case, to say, the difference between return and yield, if you read my previous blog people should understand. Return returns a value from the code block, but yield only temporarily suspends the function.


3. Can generators be nested?

def counter (start=0): While True:yield startstart+=1def repeater (): Yield 1yield 2yield from counter (ten) for Num in repeater (): If Num>100:breakprint (num)

Did you see the code? It is obvious that nested, nested syntax can be supported.

Yield from generator function


A few questions about Python builder

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.