def generator(dct):
for i in range(3):
dct[\'a\'] = i
yield dct
g = generator({\'a\': None})
next(g) # -> {\'a\': 0}
next(g) # -> {\'a\': 1}
n
They iterate over the iterator identically, but you're checking in at different points. Because dict
s are mutable, and you always yield
the same dict
, you should expect everything you yield to be identical. In your first example, you are looking at the dict
as it is changing. Instead consider
g = generator({'a': None})
a = next(g)
b = next(g)
c = next(g)
print(a, b, c)
# {'a': 2} {'a': 2} {'a': 2}