Rezha Julio

The Hard Coded Chemist

Next, Function or Method ?

2017-04-26

While in Python 2 it was possible to use both the function next() and the .next() method to iterate over the resulting values of a generator, the later has been removed with the introduction of Python 3.

Consider the sample generator:

def sample_generator():
    yield "a"
    yield "b"
    yield "c"

In Python 2:

a = sample_generator()
print(next(a)) # prints 'a'
print(a.next()) # prints 'b'

But in Python 3:

print(next(a)) # prints 'a'
print(a.next()) # AttributeError