On Fri, Mar 19, 2021 at 8:59 PM Dennis Sweeney <[email protected]>
wrote:
> I think these don't generally fit with the "feel" of the itertools module
> though since almost everything there accepts iterables and returns iterators
itertools.product as written barely seems to fit. It might as well accept
only sequences, since it immediately converts everything into a sequence
anyway.
It's also documented as "equivalent to a nested for-loop" which is plainly
untrue for several different reasons.
A simple improvement would be to keep the first argument as an uncached
iterator if repeat=1. Not only would that improve memory usage in many
cases, but it would allow for things like product(count(1), ...) which
ought to work in my opinion.
Another option is to accept iterables that repeatedly get iter() called on
> them, but that's hard to make work with generators
That variant of itertools.product (one which is truly equivalent to a
nested for loop) would have to be a new function, since it wouldn't be
backward compatible.
The lack of a convenient way to make it work with generators feels like a
hole in the standard library that should be fixed:
class ReiterableGenerator: # defined in itertools
def __init__(self, f, *args, **kwargs):
self._f, self._args, self._kwargs = f, args, kwargs
def __iter__(self):
return self._f(*self._args, **self._kwargs)
primes = ReiterableGenerator(lambda: (n for n in count(2) if
isprime(n)))
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/HQMXRFHEKEEMWHYPPQZKZXXBGKWDEI2C/
Code of Conduct: http://python.org/psf/codeofconduct/