The builder in Python

Source: Internet
Author: User

The builder in Python is actually a special iterator that needs to be designed in comparison to the generation of elements, the position of iterations, the return of values, and so on, compared to each iteration of acquiring data (via next () function). We can use a more concise syntax called the Generator (generator).

With list generation, we can create a list directly. However, if we say that we may be able to use the algorithm based on the previous 999,999 number to launch 100w, but we only need the first few numbers, then the list to save the number of 100w to spare will use a huge amount of space. But we do not necessarily use the number 100w, wasted a huge space. So we need to use the generator.

The generator does not save all the elements, and the algorithm is saved, and each call to the next method returns the value of the next element based on the algorithm. Throws a Stopiteration exception if there is no value. This has the advantage that the generator will return the number of elements according to the number you need, without wasting space. So the generator is a lazy sequence.

There are two ways to create generators:

The first is similar to the list generation, and [] is replaced with (). A This is a generator object.

The second type:

As long as a function has the keyword yield, then this function becomes the generator. The generator is turned on by next () and runs to the yield pause pending.

Yield has two functions: Save the current running state when you run to yield, and then hang.

Returns the value returned by the expression after yield, equivalent to return.

When the next () function is called again, it returns to the previously pending run where it continues to run until the next yield is run, and throws a Stopiteration exception if not.

The wake-up generator can use the Send () function in addition to the next () function. The Send () method can not only return a yield breakpoint but also pass in a value. It is important to note that the Send () method cannot be used by the generator for the first wakeup.

When the Fibonacci sequence was previously printed with an iterator, you can see that the Fibonacci sequence is calculated by a specific algorithm (a, B, a+b) to derive the next value. This is similar to the principle of the generator save algorithm, so it is easier to print the Fibonacci sequence with the generator.

  

The builder in Python

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.