[docs] [issue18612] More elaborate documentation on how list comprehensions and generator expressions relate to each other

R. David Murray report at bugs.python.org
Thu Aug 1 14:37:23 CEST 2013

R. David Murray added the comment:

Well, you haven't done anything wrong, but I'm not sure what we can do in response to the report.  What how will the answer to the question serve you?  What will you know after getting the answer that you didn't know before getting the answer?  I'm trying here to understand what it is that is missing from your mental model of Python that needs to be filled in, because the syntax and behavior of these constructs is documented, and it has never occurred to me to wonder whether or not a generator expression "is" a comprehension or not.

I'm making things up here, but I suspect we call generator expressions that because while they share the base syntax and much of the same semantics as the things we call comprehensions, (a) they produce an iterable (a generator object) instead instead of a fully realized object and (b) you can specify a generator expression without explicit surrounding punctuation (eg: "myfunc(x for x in range(7))").  That is, the 'comprehensions' are conceptually a notation for "specifying" a collection object using a compact notation, while a generator expression is a way of "creating" a generator-iterable *function* that must then be iterated to produce the collection object implicitly specified by the expression.  I'm not sure I'm being clear, because the distinction is subtle and perhaps not meaningful...the difference in naming might just be an historical accident :)

And, because it is not entirely clear, I'm not sure it is a good idea to try to document it.  Again, what enlightenment would derive from a clear explanation?

nosy: +r.david.murray
versions: +Python 2.7, Python 3.3, Python 3.4 -Python 3.5

Python tracker <report at bugs.python.org>

More information about the docs mailing list