This article mainly introduces the definition and simple usage of Python generators. It analyzes the concepts, principles, usage methods and related operation precautions of Python generators in detail in the form of examples. Friends in need can refer to it
The examples in this article describe the definition and simple usage of Python generators. Share it with everyone for your reference, the details are as follows:
1. What is a generator
In Python, due to memory limitations, List capacity is definitely limited. For example, if we create a list containing 100 million elements, Python will first open up enough space in the memory to store the list containing 100 million elements, and then allow the user to use the list. This may cause the following problems:
1. There is not enough memory space in the memory to store this list, resulting in the list being unable to be created
2. Even if the list is successfully created, it will still take a long time, resulting in program efficiency Low
3. If the user only wants to access the first few elements of the list, the space occupied by most of the elements in the subsequent list will be wasted
In order to effectively solve the above problems, Python A new mechanism of "looping and calculation" is introduced. That is, when the user needs to use an object, Python will open up the memory space and create it according to the pre-designed rules. This object is for users to use, instead of creating all the objects in advance like a list and then providing them to users for use. This mechanism is called a generator in Python.
2. Creation of the generator
A. The generator push formula
and The list push is similar, except that the generator comprehension uses () instead of [], and the generator is finally returned instead of the list
g=((i+2)**2 for i in range(2,30)) #g是一个生成器 print(g) #g为空,里面包含任何元素
Running results:
< ;generator object
at 0x0000000002263150>
B. yield keyword
If the yield keyword is included in a function definition, the function will It is no longer an ordinary function, but a generator
[Explanation]: The yield instruction can pause a function and return its intermediate results. The function using this instruction will save the execution environment , and restore if necessary
def fib(max): n,a,b=0,0,1 while n<max: #print(b) yield b a,b=b,a+b n+=1 return 'done' f=fib(6) print(f)
Running results:
# #[Note]: The difference between ordinary functions and functions that become generators:
#Ordinary functions are executed sequentially and return when encountering return or the last line of function statements. . The function that becomes a generator is executed every time the __next__() method is called, and returns when encountering a yield statement. When executed again, execution continues from the yield statement returned last time
f=fib(6) print(f) print(f.__next__()) print(f.__next__()) print('暂停一下') print(f.__next__()) print(f.__next__())Running result :
1
1
Pause for a while
2
3
3. Generator method (Reference: Bole Online)
1.close() method: Manually close the generator function, subsequent calls will directly return the StopIteration exception
def func(): yield 1 yield 2 yield 3 g=func() g.__next__() g.close() #手动关闭生成器 g.__next__() #关闭后,yield 2和yield 3语句将不再起作用Run result:
Traceback (most recent call last):File "E:\py3Demo\Hello\generatorDemo.py", line 9, in
g.__next__() #After closing, the yield 2 and yield 3 statements will no longer work
StopIteration
2.__next__() method: Return the next part of the generator One call
def func(): n=1 for i in range(3): yield n n+=1 c=func() a1=c.__next__() a2=c.__next__() a3=c.__next__()
[Process explanation]:
For ordinary generators, the first call to the __next__() method is equivalent to Start the generator. At this time, execution will start from the first line of the generator function until the yield statement (the fourth line) is executed for the first time and the generator function will jump out.
When the second __next__() method is called, the generator function will be re-entered and execution will start from the next statement (fifth line) of the yield statement until it is re-run. The yield statement jumps out of the generator function again after execution.
The following __next__() method calls are followed by analogy
3.send() method: accepts a variable passed in from the outside, And the calculation result is returned to the generator function based on the variable content
[Note]:
(1)send() method and __next__ () method is similar, the difference is that the send() method can be passed to the yield expression value, while the __next__() method cannot pass a specific value, and can only pass None to the yield expression, so This can be understood as generator.__next__() as generator.send(None)
(2)第一次调用生成器函数时,必须使用__next__()语句或是send(None),不能使用send发送一个非None的值给生成器函数,否则会出错,因为没有yield语句来接收这个值
def gen(): value=0 while True: receive=yield value if receive=='end': break value='Got:%s' %receive g=gen() print(g.__next__()) #或是print(g.send(None)),从而启动生成器 print(g.send('aaa')) print(g.send(3)) print(g.send('end'))
运行结果:
0
Got:aaa
Got:3
Traceback (most recent call last):
File "E:\py3Demo\Hello\generatorDemo.py", line 13, in
print(g.send('end'))
StopIteration
[流程解释]:
a.通过g.send(None)或g.__next__()启动生成器函数,并执行到第一个yield语句结束的位置并将函数挂起。此时执行完了yield语句,但是没有给receive赋值,因此yield value会输出value的初始值0
b.g.send('aaa')先将字符串‘aaa'传入到生成器函数中并赋值给receive,然后从yield语句的下一句重新开始执行函数(第五句),计算出value的值后返回到while头部开始新一轮的循环,执行到yield value语句时停止,此时yield value会输出‘Got:aaa',然后挂起
c.g.send(3)重复步骤b,最后输出结果为‘Got:3'
d.g.send('end')会使程序执行break然后跳出循环,从而函数执行完毕,得到StopIteration异常
4.throw()方法:向生成器发送一个异常。
def gen(): while True: try: yield 'normal value' #返回中间结果,此处的yield和return的功能相似 yield 'normal value2' print('I am here') except ValueError: print('We got ValueError') except Exception: print('Other errors') break g=gen() print(g.__next__()) print(g.throw(ValueError)) print(g.__next__()) print(g.throw(TypeError))
运行结果:
Traceback (most recent call last):
File "E:\py3Demo\Hello\generatorDemo.py", line 17, in
print(g.throw(TypeError))
StopIteration
normal value
We got ValueError
normal value
normal value2
Other errors
[解释]:
a.print(g.__next__())会输出normal value,并停在yield 'normal value2'之前
b.由于执行了g.throw(ValueError),所以回跳过后续的try语句,即yield ‘normal value2'不会执行,然后进入到except语句,打印出‘We got ValueError'。之后再次进入到while语句部分,消耗一个yield,输出normal value
c.print(g.__next__())会执行yield ‘normal value2'语句,并停留在执行完该语句后的位置
d.g.throw(TypeError)会跳出try语句,因此print('I am here')不会被执行,然后打印‘Other errors',并执行break语句跳出while循环,然后到达程序结尾,打印StopIteration异常的信息
四、生成器的运用
import time def consumer(name): print('%s准备吃包子啦!' %name) while True: baozi=yield #接收send传的值,并将值赋值给变量baozi print('包子[%s]来了,被[%s]吃了!' %(baozi,name)) def producer(name): c1=consumer('A') #把函数变成一个生成器 c2=consumer('B') c1.__next__()#调用这个方法会走到yield处暂时返回 c2.__next__() print('开始准备做包子啦!') for i in range(10): time.sleep(1) print('做了一个包子,分成两半') c1.send(i) c2.send(i) producer('Tomwenxing')
运行结果:
A准备吃包子啦!
B准备吃包子啦!
开始准备做包子啦!
做了一个包子,分成两半
包子[0]来了,被[A]吃了!
包子[0]来了,被[B]吃了!
做了一个包子,分成两半
包子[1]来了,被[A]吃了!
包子[1]来了,被[B]吃了!
做了一个包子,分成两半
包子[2]来了,被[A]吃了!
包子[2]来了,被[B]吃了!
做了一个包子,分成两半
包子[3]来了,被[A]吃了!
包子[3]来了,被[B]吃了!
做了一个包子,分成两半
包子[4]来了,被[A]吃了!
包子[4]来了,被[B]吃了!
做了一个包子,分成两半
包子[5]来了,被[A]吃了!
包子[5]来了,被[B]吃了!
做了一个包子,分成两半
包子[6]来了,被[A]吃了!
包子[6]来了,被[B]吃了!
做了一个包子,分成两半
包子[7]来了,被[A]吃了!
包子[7]来了,被[B]吃了!
做了一个包子,分成两半
包子[8]来了,被[A]吃了!
包子[8]来了,被[B]吃了!
做了一个包子,分成两半
包子[9]来了,被[A]吃了!
包子[9]来了,被[B]吃了!
相关推荐:
The above is the detailed content of Python generator definition and simple usage example analysis. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool