classification
Title: More elaborate documentation on how list comprehensions and generator expressions relate to each other
Type: Stage:
Components: Documentation Versions: Python 3.5
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: BreamoreBoy, docs@python, r.david.murray, uglemat
Priority: normal Keywords:

Created on 2013-08-01 11:23 by uglemat, last changed 2014-06-19 17:12 by uglemat. This issue is now closed.

Messages (7)
msg194048 - (view) Author: (uglemat) Date: 2013-08-01 11:23
Today there are list comprehensions, set comprehensions, dict comprehensions and then... there are generator expressions, which apparently was called 'generator comprehensions' in the original PEP, don't know why that was changed. Some questions arises:

 * Are generator expressions a type of list comprehension, or are generator expressions to be considered their own thing.

 * Does 'list comprehension' mean a type of comprehension that happens to return a list, or is it to be considered more of a general concept.

I usually talk about 'list comprehension' as a type of comprehension, and 'generator expression' as another type of comprehension, and after investigating whether or not that is correct I couldn't find an answer. On the wikipedia article on list comprension (http://en.wikipedia.org/wiki/List_comprehension#Python) they list generator expressions as well, indicating that it's a type of list comprehension. I think there's a lot of confusion to be had here, and that the documentation should clarify what exactly is meant by 'list comprehensions', regardless of what happens to be the case.

I haven't really a bug report before, so forgive me if I'm doing something wrong. :)
msg194057 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-08-01 12:37
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?
msg194064 - (view) Author: (uglemat) Date: 2013-08-01 13:00
Yeah I think the differences are pretty easy to comprehend. To be honest the reason I came here is that I had an argument where someone commented on my code (where I used a generator expression) saying something akin to "list comprehensions are nice", so I replied "actually, that's a generator expression, which are very similar but behave differently". The response was that the two concepts are one and the same. I couldn't really say that that's incorrect, but I think it's very confusing for beginners if people are talking about generator expressions like they are list expressions because they behave so differently and the documentation for list comprehensions doesn't mention generator expressions. I was kind of longing for a definite answer so I could say "Nay, thou shall differentiate between list comprehensions and generator expressions". Is it right to correct someone and say that generator expressions are in fact *not* generator expressions? Maybe I'm blowing this out of proportions. :)
msg194068 - (view) Author: (uglemat) Date: 2013-08-01 13:05
> Is it right to correct someone and say that generator expressions are in fact *not* generator expressions?

Ups, I meant "Is it right to correct someone and say that generator expressions are in fact *not* list comprehensions?"

All these expressions and comprehensions are making me dissy :)
msg194071 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-08-01 13:20
Well, it is the case that if you substitute a list comprehension for a generator expression in arbitrary code, most of the time it would work but occasionally it wouldn't, because the runtime behavior is different (lazy production versus all-at-once production).  So yes, the two are not the same thing, and it is important to understand the differences in behavior between them.  Calling a generator expression a list comprehension indicates someone's mental model of how Python works has a couple holes in it, IMO :)
msg220997 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-19 15:54
Both list comprehension and generator expression are defined in the glossary https://docs.python.org/3/glossary.html, so what else can be done?
msg221007 - (view) Author: (uglemat) Date: 2014-06-19 17:12
Yeah, I guess it's pretty obvious that generator expressions are not list comprehensions from the glossary. I'll close the bug.
History
Date User Action Args
2014-06-19 17:12:25uglematsetstatus: open -> closed

messages: + msg221007
2014-06-19 15:54:02BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220997
2013-08-01 13:20:28r.david.murraysetmessages: + msg194071
2013-08-01 13:05:31uglematsetmessages: + msg194068
2013-08-01 13:00:44uglematsetmessages: + msg194064
versions: + Python 3.5, - Python 2.7, Python 3.3, Python 3.4
2013-08-01 12:37:22r.david.murraysetnosy: + r.david.murray

messages: + msg194057
versions: + Python 2.7, Python 3.3, Python 3.4, - Python 3.5
2013-08-01 11:23:32uglematcreate