Python:为什么我应该使用next()而不是obj.next()? [英] Python: Why should I use next() and not obj.next()?

查看:110
本文介绍了Python:为什么我应该使用next()而不是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 named next 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆