CKTIS / Python
iterator.__next__
El método del iterador devuelve el siguiente elemento y lanza StopIteration al agotarse.
Versión: Python 3.14; repository image, production runtime not attested
Sintaxis
item = next(iterator)El método del iterador devuelve el siguiente elemento y lanza StopIteration al agotarse.
iterator.__next__
next(it) usa el protocolo, next(it, default) sustituye el agotamiento por el valor indicado. None puede ser un elemento normal. Un iterador correcto permanece agotado.
La primera llamada da 1, list consume el resto [0] y la final da end.
it=iter([None])
assert next(it) is None
for _ in range(2):
try: next(it)
except StopIteration: pass
else: raise AssertionError
Ejemplos
class Countdown:
def __init__(self, n): self.n = n
def __iter__(self): return self
def __next__(self):
if self.n <= 0: raise StopIteration
self.n -= 1
return self.n
it = Countdown(2)
print(next(it), list(it), next(it, "end"))
Resultado
1 [0] end
La primera llamada da 1, list consume el resto [0] y la final da end.