Python:为什么我应该使用next()而不是obj.next()? [英] Python: Why should I use next() and not obj.next()?
问题描述
Python 2.6引入了next
函数.
Python 2.6 introduced a next
function.
为什么这是必要的?总是可以键入obj.next()
而不是next(obj)
.
Why was this necessary? One could always type obj.next()
instead of next(obj)
.
后者是否更多pythonic
?
推荐答案
PEP 3114 对此进行了描述改变.动机摘录:
PEP 3114 describes this change. An excerpt about the motivation:
此PEP建议将
next
方法重命名为__next__
, 与Python中所有其他协议保持一致,其中方法是 作为语言级协议的一部分被隐式调用,并且 引入名为next
的内置函数来调用__next__
方法, 与其他协议的显式方式一致 调用.
This PEP proposes that the
next
method be renamed to__next__
, consistent with all the other protocols in Python in which a method is implicitly called as part of a language-level protocol, and that a built-in function namednext
be introduced to invoke__next__
method, consistent with the manner in which other protocols are explicitly invoked.
请务必阅读此PEP,以获取更多有趣的细节.
Be sure to read this PEP for more interesting details.
关于您为什么要使用内置的next
的原因:一个很好的原因是next
方法在Python 3中消失了,因此对于可移植性,最好开始使用next
尽快内置.
As for why you want to use the next
built-in: one good reason is that the next
method disappears in Python 3, so for portability it's better to start using the next
built-in as soon as possible.
这篇关于Python:为什么我应该使用next()而不是obj.next()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!