Python Non-return _ generator

Source: Internet
Author: User

Builder: A generator is a special type of function that generates a value at a time. It can be treated as a recoverable function. Calling this function will return a x generator "Generator" that can be used to generate successive values, and the generator has the ability to generate on one side of the computation, saving space and really being able to take it on demand. Take a look at the following code:

[x**2  forXinchRange -) ]
Output result: [0,1,4,9, -, -, $, the, -,Bayi, -,121,144,169,196,225, the,289,324,361]
---------------------------------------------------------------------------------------
A = [x**2 for x in range (20)]
Type (A)
Output Result:
<class ' list ' >
---------------------------------------------------------------------------------------
b= (x**2 for x in range (20))
Print (B)
Output Result:
<generator Object <genexpr> at 0x0000015c857d7eb8>

We can see that the output of a is a list, so let's just imagine that when the number of elements in the list is large, it takes up much of the system space and increases the CPU load. and B's output is the generator's memory space, but we also want to get the output of a, how to do? We can use __next__ () to invoke the element, each time __next__ () is executed, and one element is called back.

b= (x**2 for x in range (20))
Print (B.__next__()) Print (B.__next__()) Print (B.__next__()) Print (B.__next__())
Output Result:
0 # X=0
1 # X=1
4 # x=2
9 # X=3

Does it bother to call one element at a time if there are more elements? So __next__ is basically not used and uses a for loop to iterate. Generator is very powerful, if the algorithm is more complex, with a similar list of generated for loops can not be implemented, but also with the function. Functions that typically have a yield keyword are called generators.

Look at this code.

def func1 (x):      while x < a:        = x**2        yield  n        x+=1F=func1 ($)   for in F:    print(i)

The func1 (x) function is a generator, and when you run the FUNC1 (x), the function becomes the generator and the value is called each time through yield.

Summarize:

Generally the function with yield is the generator, the generator uses the method __next__ (), and only one value can be adjusted at a time, if you want to tune multiple values, you can use for to iterate over the values. The generator can make the on-demand value, reducing the effect of the load.

-------'s not going to develop OPS, not a good cook.

Python Non-return _ generator

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.