Home > Backend Development > Python Tutorial > Detailed explanation of yield and generator example codes in python

Detailed explanation of yield and generator example codes in python

高洛峰
Release: 2017-03-15 14:01:47
Original
1738 people have browsed it

First we import from a small program, each specify a list, and find the prime numbers in it, we will write like this

import math
def is_Prims(number):

    if number == 2:
        return True
    //除2以外的所有偶数都不是素数
    elif number % 2 == 0:
        return False
    //如果一个数能被除1和本身之外的数整除,则为合数。其实我们的判定范围到根号n就可以
    for cur in range(2,int(math.sqrt(number))+1,2):
        if number % cur == 0:
            return False
        else:
            return True

def get_Prims(input_list):

    result_list = list()
    for element in input_list:
        if is_Prims(element):
            result_list.append(element)
    return result_list

aa = get_Prims([1,2,3,4,5,6,7,8,9])
print (aa)
Copy after login

But if we want to give a number, then list What about all the prime numbers greater than this number? We might write like this:

def get_Prims(number):
    if is_Prims(number):
            return number
Copy after login

But once the returnfunction ends completely after handing control to the caller, any local variablesand function work are discarded. Next A call will start from scratch again. So we can use the following writing method:

def get_Prims(number):
    while(True):
        if is_Prims(number):
            yield number
        number += 1

def get_numbers():
    total = list()
    for next_prim in get_Prims(2):
        if next_prim < 100:
            total.append(next_prim)
        else:
            print(total)
            return

get_numbers()
Copy after login

The generator function is explained below. The def code of a function contains yield, and the function automatically becomes a generator function (even if it still contains return), the generator function creates Generator (a special form of iterator, this iterator has a built-in next() method), when a value is needed, it is generated through yield instead of direct return. Therefore, unlike ordinary functions, the control right is not Not handed over.

for loop will implicitly call the next() function. The next() function is responsible for calling the next() method in the generator. At this time, the generator is responsible for returning a value to any call to next( ) method, use yield to transfer this value back, which is equivalent to the return statement.


The above is the detailed content of Detailed explanation of yield and generator example codes in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template