5. Python3 中的生成器的高级方法 及总结

回顾:

  • 生成器就是含有yield操作的函数,生成器函数的返回值就是一个迭代器(generator object)

主要内容:

  • 外部如何给生成器发送一个值,临时按需变化迭代生成值。(send方法)
  • 生成器返回的迭代器,一般只能使用一次。可采用内在的异常机制对第二次调用报错。
  • 生成器“对象”(生成器产生的迭代器对象)的close方法示例
  • 个人理解与总结

5.1 send 函数

  • 错误的例子
>>> def simpleGen(value): # 定义了一个简单的生成器
...     while True:
...         yield value
... 
>>> gen = simpleGen(42);
>>> print(gen.__next__(),gen.__next__())
42 42
>>> gen.send(10);
42
>>> print(gen.__next__(),gen.__next__())
42 42

yield 此处是一个语句。然而我们需要在生成器函数中对gen.send(10);进行响应,那么我们就要使用yield 表达式, yield 不再是语句,而是表达式!!!

  • 正确的例子
#正确的例子
>>> def simpleGen(value):
...     new = None;
...     i = 0;
...     while True:
...         i +=1
...         print('In Func (before):',i, new, value)
...         new = yield value
...         print('In Func (after):',i, new, value)
...         value = new;
... 
>>> gen = simpleGen(42); # nothing happend 
>>> print(gen.__next__()) # called 1st, get 42
In Func (before): 1 None 42
42
>>> print(gen.__next__()) # called 2nd, get None, this is important!
In Func (after): 1 None 42
In Func (before): 2 None None
None
>>> print(gen.send(10));    # called 3rd, get what you set
In Func (after): 2 10 None
In Func (before): 3 10 10
10
>>> print(gen.__next__()) # called 4th, get None, this is important!
In Func (after): 3 None 10
In Func (before): 4 None None
None

此处应该有结论:

  1. 构造函数啥都不干,除了赋值
  2. send函数和__next__函数一样,都唤醒了沉睡中的yield表达式
  3. yield表达式的值在正常情况下是None
  • 最终更佳的例子(处理None)
>>> def simpleGen(value):
...     while True:
...         new = yield value
...         if new is not None:
...              value = new;
... 
>>> gen = simpleGen(42); # nothing happend 
>>> print(gen.__next__()) # called 1st, get 42
42
>>> print(gen.__next__()) # called 2nd, get 42
42
>>> print(gen.send(10));    # called 3rd, get what you set
10
>>> print(gen.__next__()) # called 4th, also get what you set
10

5.2 throw 方法

这个很简单,就是通过语句,然生成器内部产生异常,注意异常报出的位置(yield 语句)

>>> def simpleGen(value):
...     while True:
...         new = yield value
...         if new is not None:
...              value = new;
... 
>>> gen = simpleGen(42); # nothing happend 
>>> print(gen.__next__()) # called 1st, get 42
42
>>> gen.throw(StopIteration)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in simpleGen ## 注意异常报出的位置(yield 语句)。
StopIteration

5.3 close 语句

显然 throw方法产生的异常并没有在内部处理掉,直接立马传递到上一层了。我们能否设置下迭代生成器的终止,也就是不能产生下一个了。那就是无参数的close方法。

>>> def simpleGen(value):
...     while True:
...         new = yield value
...         if new is not None:
...              value = new;
... 
>>> gen = simpleGen(42); # nothing happend 
>>> print(gen.__next__()) # called 1st, get 42
42
>>> gen.close() # 终止了这个迭代器,但是没有异常
>>> print(gen.__next__())  # 继续取值,便产生了StopIteration的异常
Traceback (most recent call last):
  File "", line 1, in 
StopIteration

5.4 小结下:

学习迭代器和生成器有一两天了,自己也编程实现了大部分的功能。其目的都是为了高效的迭代循环而生的。那么循环的处理方式有如下几种:

  • 直接采用元组、列表、字符串
  • 利用内置的迭代器(xrange函数等)
  • 自己构造类(迭代器)(__next__,__iter__)
  • 使用yield函数(生成器)(yield,__next__,send, throw, close)

感受,在表达形式上,Python确实和c、c++语言有较大差别。但是,这种迭代器的核心思想在C、C++中是以指针、引用等复杂的方式实现的,Python肯定是牺牲了一定的计算效率将其封装成为了迭代器和生成器,带来的好处是指针和内存的安全,编程效率的提升,所以python是一门很好的科学计算语言,使得开发者能够注重算法层面的开发,而不是指针内存等烦人细节。

你可能感兴趣的:(5. Python3 中的生成器的高级方法 及总结)