Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More elaborate documentation on how list comprehensions and generator expressions relate to each other #62812

Closed
uglemat mannequin opened this issue Aug 1, 2013 · 7 comments
Labels
docs Documentation in the Doc dir

Comments

@uglemat
Copy link
Mannequin

uglemat mannequin commented Aug 1, 2013

BPO 18612
Nosy @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2014-06-19.17:12:25.017>
created_at = <Date 2013-08-01.11:23:32.895>
labels = ['docs']
title = 'More elaborate documentation on how list comprehensions and generator expressions relate to each other'
updated_at = <Date 2014-06-19.17:12:25.015>
user = 'https://bugs.python.org/uglemat'

bugs.python.org fields:

activity = <Date 2014-06-19.17:12:25.015>
actor = 'uglemat'
assignee = 'docs@python'
closed = True
closed_date = <Date 2014-06-19.17:12:25.017>
closer = 'uglemat'
components = ['Documentation']
creation = <Date 2013-08-01.11:23:32.895>
creator = 'uglemat'
dependencies = []
files = []
hgrepos = []
issue_num = 18612
keywords = []
message_count = 7.0
messages = ['194048', '194057', '194064', '194068', '194071', '220997', '221007']
nosy_count = 4.0
nosy_names = ['r.david.murray', 'docs@python', 'BreamoreBoy', 'uglemat']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue18612'
versions = ['Python 3.5']

@uglemat
Copy link
Mannequin Author

uglemat mannequin commented Aug 1, 2013

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. :)

@uglemat uglemat mannequin assigned docspython Aug 1, 2013
@uglemat uglemat mannequin added the docs Documentation in the Doc dir label Aug 1, 2013
@bitdancer
Copy link
Member

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?

@uglemat
Copy link
Mannequin Author

uglemat mannequin commented Aug 1, 2013

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. :)

@uglemat
Copy link
Mannequin Author

uglemat mannequin commented Aug 1, 2013

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 :)

@bitdancer
Copy link
Member

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 :)

@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Jun 19, 2014

Both list comprehension and generator expression are defined in the glossary https://docs.python.org/3/glossary.html, so what else can be done?

@uglemat
Copy link
Mannequin Author

uglemat mannequin commented Jun 19, 2014

Yeah, I guess it's pretty obvious that generator expressions are not list comprehensions from the glossary. I'll close the bug.

@uglemat uglemat mannequin closed this as completed Jun 19, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

1 participant