今天我们来介绍下Python基础学习教程之iter 方法另外的用法。据说很少有人知道这个用法!
一、上程式码、学用法
我们都比较熟悉 iter(obj),会返现一个迭代器,如果 obj 不是可迭代物件,则会报错。但其实如果仔细看官方文件,会发现 iter 方法其实是接受两个引数的,文件说明如下
iter(object[, sentinel])
sentinel 英文翻译为 哨兵。
sentinel 引数是可选的,当它存在时,object 不再传入一个可迭代物件,而是一个可呼叫物件,通俗点说就是可以通过呼叫的物件,而 sentinel 的作用就和它的翻译一样,是一个“哨兵”,当时可呼叫物件返回值为这个“哨兵”时,循环结束,且不会输出这个“哨兵”。
可能有点难懂,用一个简单需求来说明,需求说明如下:
心里想一个[1, 10]范围的数,然后程式码开始随机,当随机到想的数时停止,看每次程式码需要随机几次。
实现分析:看起来应该很简单,random,然后加一个if判断即可,但是用 iter 来实现更简单。实现程式码如下:
from random import randint
def guess:
return randint(0, 10)
num = 1
# 这里先写死心里想的数为5
for i in iter(guess, 5):
print("第%s次猜测,猜测数字为: %s" % (num, i))
num += 1
# 当 guess 返回的是 5 时,会丢掷异常 StopIteration,但 for 循环会处理异常,即会结束循环
二、还是看看文件吧
关于这两个引数,文件里也说的很详细,分段解释如下:
The first argument is interpreted very differently depending on the presence of the second argument.
翻译:第一个引数根据第二个引数有不同的含义
Without a second argument, object must be a collection object which supports the iteration protocol (the _iter_ method), or it must support the sequence protocol (the _getitem_ method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised.
翻译:如果没有第二个引数,object(即第一个引数)是一个支援迭代器协议(实现_iter_方法的)的集合物件,或者是支援序列协议(实现_getitem_方法)且是从0开始索引。如果它不支援其中任何一个,则丢掷 TypeError 异常
简单来说就是,如果没有第二个引数,就是我们比较熟悉的用法。程式码示例如下:
In [5]: iter("123")
Out[5]:
In [6]: iter([1, 2, 3])
Out[6]:
In [7]: iter(123)
TypeError Traceback (most recent call last)
in
----> 1 iter(123)
TypeError: \'int\' object is not iterable
再来看看有第二个引数的情况
If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its _next_ method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
翻译:如果给定了第二个引数 sentinel,object 则必须是一个可呼叫物件,这个可呼叫物件没有任何引数,当可呼叫物件的返回值等于 sentinel 的值时,丢掷 StopIteration 的异常,否则返回当前值。(这里如果不好理解可呼叫物件,可以理解为函式,这样更容易想明白)
对于这个用法的适用场景,文件中也给出了说明:
One useful application of the second form of iter is to build a block-reader. For example, reading fixed-width blocks from a binary database file until the end of file is reached:
翻译:对于第二个引数,一个有用的场景是建立一个 blokc-reader,即根据条件中断读取。比如:从二进位制数据库档案读取固定宽度的块,直到到达档案的末尾,程式码示例如下:
from functools import partial
with open(\'mydata.db\', \'rb\') as f:
for block in iter(partial(f.read, 64), b\'\'):
process_block(block)
三、小结一下
1、iter 方法不管有没有第二个引数,返回的都是迭代器
2、iter 方法第一个引数的引数型别,根据有无第二个引数决定





























