我注意到 Python2.6 在它的全局函数列表中添加了一个 next() 。
Retrieve the next item from the iterator by calling its next() method.如果
default给出,则在迭代器耗尽时返回,否则StopIteration引发。
添加这个的动机是什么?你能用你next(iterator)不能做的事情iterator.next()和except处理 StopIteration 的子句做什么?
我注意到 Python2.6 在它的全局函数列表中添加了一个 next() 。
Retrieve the next item from the iterator by calling its next() method.如果
default给出,则在迭代器耗尽时返回,否则StopIteration引发。
添加这个的动机是什么?你能用你next(iterator)不能做的事情iterator.next()和except处理 StopIteration 的子句做什么?
这只是为了与len(). 我相信内部next(i)调用i.__next__()。
请注意,在 Python 3.0+ 中,该next方法已重命名为__next__. 这是因为一致性。next是一种特殊方法,特殊方法按约定命名(PEP 8),带有双前导和尾随下划线。特殊方法并不意味着直接调用,这就是next引入内置函数的原因。
它在内部调用__next__,但它使它看起来比“面向对象”更“功能性”。请注意,这只是我的意见,但我不喜欢next(i)而不是i.next()。但正如史蒂夫麦克所说,它也有助于保持一致性。