what's the only one obvious way?

Michele Simionato michele.simionato at gmail.com
Sun Jul 11 09:32:38 CEST 2004

```This came up in the Italian newsgroup; I take the occasion to bring
the question here since it is something which puzzled me for a while.
The question is when we are expected to use itertools and when we
are expected to use generator expressions. Consider these examples:

# from itertools recipes in the 2.4a1 docs
def all(seq, pred=bool):
"Returns True if pred(x) is True for every element in the iterable"
return False not in imap(pred, seq)

def any(seq, pred=bool):
"Returns True if pred(x) is True at least one element in the iterable"
return True in imap(pred, seq)

# alternative versions
def all(seq, pred=bool):
return False not in (pred(x) for x in seq)

def any(seq, pred=bool):
return True in (pred(x) for x in seq)

I maintain that it is *not obvious* to prefer one choice over the other
and this is BAD. So where are the style-guides? Is the itertools module
going to be deprecated even before it is finished ??

The point is that the mindset to use itertools is pretty different
from the mindset to use generator expressions, in the same sense
of the difference between map, filter and list comprehensions.

It seems to me that list comprehensions won; also Guido publicly said
and repeated that he never liked the contributed functional stuff (filter,
map, reduce, lambda) so there is a guidance here (even if I will continue
to use them no matter what Guido says ;)

But if a younger pythonista ask me what should he use between
itertools and generator comprehensions I don't know what to say
(of course I suggest him the more readable solution but in the
example before it is purely subjective to decide which is the more