From the yield of Python,

Source: Internet
Author: User

While reading the source of the Wiki module in Trac, I discovered that the key word of yiled was used in many places.

The feeling is to replace return with yield in the place where you need to return a value,

Not very clear about its usage, so study it carefully.

A function that uses the Yiled keyword is no longer a normal function, but a generator function (generator functions).

An iterator (iterator) is returned when the function is called.

So the two concepts of iterators and generators are explained below.

I. iterators (Iterator)

Writes that an iterator is an object that implements an iterator protocol,
There are generally two ways to implement
1) Next method
Returns the next element of the container
2) __iter__ method
Returns the iterator itself

For the for language you may not be unfamiliar, we often need to traverse some container objects will be used to the FOR loop:

Python code
    1. l=[0,1,2,3,4,5,6]
    2. For I in L:
    3. Print I

L is an object of type list, this section of the For-in code is actually called the __iter__ () function of L when it is running, returns an implementation of __next__ () or next (each version of Python may not be the same, I experimented with an iterator object that used the version 2.6.2), and each time the loop was taken to remove an element from next.

Of course, there is no need to put all the elements into a list or other container class to cycle, which is a waste of space.

We can create one of our own iterators directly.

Python code
  1. #-*-Coding:utf-8-*-
  2. "' 'Fibonacci iterator '
  3. Class Fib:
  4. " an iterator that can generate a Fibonacci sequence"
  5. def __init__ (self, max):
  6. Self.max = Max
  7. def __iter__ (self):
  8. SELF.A = 0
  9. self.b = 1
  10. return self
  11. def next (self):
  12. FIB = SELF.A
  13. if fib > self.max:
  14. Raise Stopiteration
  15. SELF.A, self.b = self.b, self.a + self.b
  16. return fib

With this Fibonacci iterator defined, we're ready to use it.

Python code
    1. From Fibonacci2 import Fib
    2. For-N in Fib:
    3. Print n

When the FIB (1000) is called, an iterator object is generated, each time the loop is called next to fetch the next value.

So we can see that the iterator has a very central thing is that in the loop, the iterator can remember the previous state.

Two. Generator

As we said before, any function that uses the yield keyword is no longer a normal function, so let's take a look at the example, which is easier to understand.

Python code
    1. def fib (max):
    2. A, B = 0, 1
    3. While a < max:
    4. yield a
    5. A, B = B, A + b

Here a few lines of code implement the above iterator class so a lot of code implemented by the function

It is very similar to the above when used:

Python code
    1. From Fibonacci import fib
    2. For-N in fib:
    3. Print n

The citation fib function uses yield so it is a generator function, and when we call the FIB (1000) It actually returns an iterator, and this iterator can control the operation of the generator function.

We control the operation of the FIB generator function through the action of the returned iterator.

Whenever the next function of the iterator is called, the generator function runs to the yield, returns the value after yield and pauses in this place, and all States are held until the next function is called, or an exception loop exits.

So the concept of generator is still very simple.

Three. Summary

1.for-in statements operate on an iterator object at the bottom.

2. A function that uses the yield keyword is a generator function that, when called, generates an iterator that can control its own run.

From the yield of Python,

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.