为什么 Python 2.6 增加了全局 next() 函数? [英] Why did Python 2.6 add a global next() function?
问题描述
我注意到 Python2.6 添加了 next() 到它的全局函数列表一>.
I noticed that Python2.6 added a next() to it's list of global functions.
Retrieve the next item from the iterator by calling its next() method.
如果给定了 default
,则返回如果迭代器耗尽,否则StopIteration
被引发.
If default
is given, it is returned if
the iterator is exhausted, otherwise
StopIteration
is raised.
添加这个的动机是什么?你可以用 next(iterator)
做什么而你不能用 iterator.next()
和一个 except
子句来处理 StopIteration?
What was the motivation for adding this?
What can you do with next(iterator)
that you can't do with iterator.next()
and an except
clause to handle the StopIteration?
推荐答案
这只是为了与 len()
等函数保持一致.我相信 next(i)
在内部调用 i.__next__()
.
It's just for consistency with functions like len()
. I believe next(i)
calls i.__next__()
internally.
参见 http://www.python.org/dev/peps/pep-3114/
这篇关于为什么 Python 2.6 增加了全局 next() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!