itertools product(infinite iterator) hangs

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Sep 13 22:26:24 EDT 2019


I've been staring at this for a little while:

from itertools import product

class Naturals:
    def __iter__(self):
        i = 1
        while True:
            yield i
            i += 1

N = Naturals()
print(iter(N))
print(product(N))  # <--- hangs

When I run the above the call to product hangs but I can't see why. I
would expect that since I'm not iterating over the product it would
just call iter(N) but clearly not since iter(N) returns a generator
instantly where as product(N) hangs.

What am I missing?

Oscar



More information about the Python-list mailing list