You only create one ndarray. When you do nc += 1
, you modify it in place. So the later iterations modify the same array already yielded, and then yield it again.
You can see this by printing out the generated arrays one at a time:
>>> it = iterable(3)
>>> x = next(it)
>>> print x
[0 0 0]
>>> y = next(it)
>>> print y
[1 1 1]
>>> print x # x changed!
[1 1 1]
>>> x is y
True
You can see that the second iteration changes the object that was yielded on the first iteration, because they are the same object.
To fix it, change your nc += 1
line to nc = nc + 1
. This will create a new array on each iteration.