导入next()python 2.5 [英] import next() python 2.5
本文介绍了导入next()python 2.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用itertools中成对配方的略微更改版本,如下所示
I'm using a slightly altered version of the pairwise recipe from itertools which looks like this
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return zip(a, b)
现在事实证明,我需要使用 python 2.5 运行代码,其中next()函数将引发以下异常:
Now it turns out I need to run the code with python 2.5 where the next() function throws the following exception:
<type 'exceptions.NameError'>: global name 'next' is not defined
是否可以在python 2.5中使用next()?还是我需要修改该功能以使其正常工作?
Is there a way to use next() with python 2.5? Or how do I need to modify the function to make it work anyhow?
推荐答案
您可以轻松地自己提供此函数的定义:
You can easily provide a definition of this function yourself:
_sentinel = object()
def next(it, default=_sentinel):
try:
return it.next()
except StopIteration:
if default is _sentinel:
raise
return default
这篇关于导入next()python 2.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文