470,849 Members | 1,240 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

what's the only one obvious way?

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
Jul 18 '05 #1
1 1127
mi***************@gmail.com (Michele Simionato) writes:
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.
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
33 posts views Thread by Diez B. Roggisch | last post: by
92 posts views Thread by Reed L. O'Brien | last post: by
137 posts views Thread by Philippe C. Martin | last post: by
18 posts views Thread by Jm | last post: by
267 posts views Thread by Xah Lee | last post: by
669 posts views Thread by Xah Lee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.