Python 2.6 之前版本的 def next() ? (而不是 object.next 方法)

发布于 2024-08-11 01:15:43 字数 107 浏览 3 评论 0原文

Python 2.6+ 和 3.* 有 next(),但 2.6 之前的版本仅提供 object.next 方法。有没有办法在2.6之前获得next()样式;也许是一些“def next():”构造?

Python 2.6+ and 3.* have next(), but pre-2.6 only offers the object.next method. Is there a way to get the next() style in pre-2.6; some "def next():" construction perhaps?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海蓝天 2024-08-18 01:15:43
class Throw(object): pass
throw = Throw() # easy sentinel hack
def next(iterator, default=throw):
  """next(iterator[, default])

  Return the next item from the iterator. If default is given
  and the iterator is exhausted, it is returned instead of
  raising StopIteration.
  """
  try:
    iternext = iterator.next.__call__
    # this way an AttributeError while executing next() isn't hidden
    # (2.6 does this too)
  except AttributeError:
    raise TypeError("%s object is not an iterator" % type(iterator).__name__)
  try:
    return iternext()
  except StopIteration:
    if default is throw:
      raise
    return default

throw = object() 也可以,但是这在检查时会生成更好的文档,例如 help(next)None 不合适,因为您必须以不同的方式对待 next(it)next(it, None)。)

class Throw(object): pass
throw = Throw() # easy sentinel hack
def next(iterator, default=throw):
  """next(iterator[, default])

  Return the next item from the iterator. If default is given
  and the iterator is exhausted, it is returned instead of
  raising StopIteration.
  """
  try:
    iternext = iterator.next.__call__
    # this way an AttributeError while executing next() isn't hidden
    # (2.6 does this too)
  except AttributeError:
    raise TypeError("%s object is not an iterator" % type(iterator).__name__)
  try:
    return iternext()
  except StopIteration:
    if default is throw:
      raise
    return default

(throw = object() works too, but this generates better docs when inspecting, e.g. help(next). None is not suitable, because you must treat next(it) and next(it, None) differently.)

微凉徒眸意 2024-08-18 01:15:43

R. Pate 似乎有一个很好的答案。需要额外补充一点:如果您正在编写在许多不同版本的 Python 上运行的代码,您可以对定义进行条件化:

try:
    next = next
except NameError:
    def next():
        # blah blah etc

这样您在任何情况下都定义了 next ,但您使用的是内置实现(如果可用)。

我使用 next = next 以便可以将此定义放入模块中,然后在代码的其他位置使用:

from backward import next

R. Pate seems to have a good answer. One extra bell to add: if you're writing code to run on many different versions of Python, you can conditionalize the definition:

try:
    next = next
except NameError:
    def next():
        # blah blah etc

That way you have next defined in any case, but you're using the built in implementation where it's available.

I use next = next so that I can put this definition in a module, then elsewhere in my code use:

from backward import next
迎风吟唱 2024-08-18 01:15:43

更简单的方法:

import operator

next = operator.methodcaller("next")

Ned 关于将其放入 try 块的建议在这里也适用,但如果您打算走这条路,请注意一点:在 Python 3 中,调用 next()非迭代器上的 会引发 TypeError,而此版本会引发 AttributeError

编辑:没关系。正如 steveha 指出的,operator.methodcaller() 是在 2.6 中才引入的,这很遗憾。

Simpler method:

import operator

next = operator.methodcaller("next")

Ned's suggestion about putting it in a try block works here as well, but if you're going to go that route, one minor note: in Python 3, calling next() on a non-iterator raises a TypeError, whereas this version would raise an AttributeError instead.

Edit: Never mind. As steveha points out, operator.methodcaller() was only introduced in 2.6, which is a shame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文