Home  >  Article  >  Backend Development  >  Python generator definition and simple usage example analysis

Python generator definition and simple usage example analysis

不言
不言Original
2018-05-02 16:04:571163browse

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 722fac25613fec5eb1c1df199f557dc1 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 &#39;done&#39;
f=fib(6)
print(f)

Running results:

5803358a1103d13381d03415effc78ac

# #[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(&#39;暂停一下&#39;)
print(f.__next__())
print(f.__next__())

Running result :

0a97eedc01b1b40c161307d66d599fc6

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 4225fa317875f3e92281a7b1a5733569
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==&#39;end&#39;:
      break
    value=&#39;Got:%s&#39; %receive
g=gen()
print(g.__next__()) #或是print(g.send(None)),从而启动生成器
print(g.send(&#39;aaa&#39;))
print(g.send(3))
print(g.send(&#39;end&#39;))

运行结果:

0
Got:aaa
Got:3
Traceback (most recent call last):
  File "E:\py3Demo\Hello\generatorDemo.py", line 13, in 4225fa317875f3e92281a7b1a5733569
    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 &#39;normal value&#39; #返回中间结果,此处的yield和return的功能相似
      yield &#39;normal value2&#39;
      print(&#39;I am here&#39;)
    except ValueError:
      print(&#39;We got ValueError&#39;)
    except Exception:
      print(&#39;Other errors&#39;)
      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 4225fa317875f3e92281a7b1a5733569
    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(&#39;%s准备吃包子啦!&#39; %name)
  while True:
    baozi=yield #接收send传的值,并将值赋值给变量baozi
    print(&#39;包子[%s]来了,被[%s]吃了!&#39; %(baozi,name))
def producer(name):
  c1=consumer(&#39;A&#39;) #把函数变成一个生成器
  c2=consumer(&#39;B&#39;)
  c1.__next__()#调用这个方法会走到yield处暂时返回
  c2.__next__()
  print(&#39;开始准备做包子啦!&#39;)
  for i in range(10):
    time.sleep(1)
    print(&#39;做了一个包子,分成两半&#39;)
    c1.send(i)
    c2.send(i)
producer(&#39;Tomwenxing&#39;)

运行结果:

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]吃了!

相关推荐:

Python迭代器定义与简单用法分析

Python装饰器原理与用法分析


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!

Statement:
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