what's the only one obvious way?

M

Michele Simionato

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
readable solution).
Can somebody channel Guido thoughts on this issue, please?

Thanks,


Michele Simionato
 
P

Paul Rubin

Can somebody channel Guido thoughts on this issue, please?

Use generator expressions. The whole existence of an add-on itertools
module pointed to something being missing from the base language.
That something was generator expressions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top