Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import next() python 2.5

Tags:

python

next

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) 

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

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?

like image 901
LarsVegas Avatar asked Jul 25 '12 14:07

LarsVegas


1 Answers

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
like image 126
Sven Marnach Avatar answered Oct 14 '22 06:10

Sven Marnach