Python: next () not recognized

When I do next(ByteIter, '')<<8in python, I got a name error saying

"global name" next "undefined"

I assume this function is not recognized due to python version? My version is 2.5.

+2
source share
3 answers

From documents

next (iterator [, default])

Retrieve the next item from the iterator by calling its next() method. 
If default is given, it is returned if the iterator is
exhausted, otherwise StopIteration is raised.

New in version 2.6.

So yes, this requires version 2.6.

+3
source

although you can call ByteIter.next () in 2.6. However, this is not recommended, as the method has been renamed in python 3 to the following ().

+1
source

next() ​​ Python 2.6.

. .next() Python 2:

try:
    ByteIter.next() << 8
except StopIteration:
    pass

.next() StopIteration, , StopIteration.

:

_sentinel = object()
def next(iterable, default=_sentinel):
    try:
        return iterable.next()
    except StopIteration:
        if default is _sentinel:
            raise
        return default

This works the same as Python 2.6:

>>> next(iter([]), 'stopped')
'stopped'
>>> next(iter([]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in next
StopIteration
+1
source

All Articles