Python yield generator

Source: Internet
Author: User
Keywords python generator python yield generator
yield: generator
Any function that uses yield is called a generator, such as:


def count(n):
    while n> 0:
        yield n #Generate value: n
        n -= 1
 

Another way of saying: a generator is a function that returns an iterator. The difference from a normal function is that the generator contains a yield statement. It is easier to understand that a generator is an iterator.

Using yield, you can let the function generate a sequence. The object type returned by the function is "generator", and the next () method is continuously called by the object to return the sequence value.

c = count(5)
c.next()
>>> 5
c.next()
>>>4
 

The generator function starts executing the statements in the function only when the next() method is called, for example:


def count(n):
    print "cunting"
    while n> 0:
        yield n #Generate value: n
        n -= 1
 

When calling the count function: c=count(5), it does not print "counting". Only when calling c.next(), the statement inside is actually executed. Each time the next() method is called, the count function will run until the statement yield n is reached. The return value of next() is the generated value n. When the next() method is called again, the function continues to execute the statement after yield (familiar with Java Friends must know the Thread.yield() method, which is used to suspend the current thread and let other threads execute), such as:


def count(n):
    print "cunting"
    while n> 0:
        print'before yield'
        yield n #Generate value: n
        n -= 1
        print'after yield'
 

The above code does not print "after yield" when the next method is called for the first time. If you always call the next method, the program will report an error when there is no iterable value:

Traceback (most recent call last): File "", line 1, in StopIteration

So the next method is generally not called manually, but the for loop is used:

for i in count(5):
    print i,
 

Example: Use the yield generator to simulate the command in Linux: tail -f | grep python is used to find the line with python in the monitoring log file.

import time
def tail(f):
    f.seek(0,2)#Move to file EOF, reference: [seek](http://docs.python.org/2/library/stdtypes.html?highlight=file#file.seek)
    while True:
        line = f.readline() #Read the new text line in the file
        if not line:
            time.sleep(0.1)
            continue
        yield line
  
def grep(lines, searchtext):
    for line in lines:
        if searchtext in line:
            yield line
 

transfer:


flog = tail(open('warn.log'))
pylines = grep(flog,'python')
for line in pylines:
    print line,
 

Use yield to implement the Fibonacci sequence:


def fibonacci():
    a=b=1
    yield a
    yield b
    while True:
        a,b = b,a+b
        yield b
 

transfer:


for num in fibonacci():
    if num> 100:
        break
    print num,
 

The role of return in yield:
As a generator, because it returns a value every iteration, it is not possible to return a value in the generator function, including the None value, otherwise it will throw a "SyntaxError" exception, but it can appear in the function A separate return indicates the end of the statement.
Example of reading files continuously through a fixed-length buffer to prevent memory overflow in one-time reading:


def read_file(path):
    size = 1024
    with open(path,'r') as f:
        while True:
            block = f.read(SIZE)
            if block:
                yield block
            else:
                return
 

If a specific value is returned in the function, an exception is thrown directly


>>> def test_return():
... yield 4
... return 0
...
  File "<stdin>", line 3
SyntaxError:'return' with argument inside 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.