[Python-Dev] Naming comprehension syntax [was Re: Informal educator feedback on PEP 572 ...]
Steven D'Aprano
steve at pearwood.info
Tue Jul 3 20:10:27 EDT 2018
On Wed, Jul 04, 2018 at 11:13:20AM +1200, Greg Ewing wrote:
> Terry Reedy wrote:
> >If we had followed the math precedent, instead of <other computer
> >language>, we would have set builders, list builders, dict builders, and
> >generator builders.
>
> I was intending to suggest something like that back when
> comprehensions were first being discussed, but people
> raced ahead and adopted the term "comprehension" before
> I got the chance.
>
> "List builder" and "dict builder" make a lot of sense,
> but "generator builder" not so much -- it *is* a generator,
> not something that builds a generator. In fact it doesn't
> build anything in the sense that the others do. So maybe
> "generator expression" is the best we could have done.
But [expr for x in seq] is a list, just as (expr for ...) is a
generator. If you don't believe me, try it:
py> type([x for x in (1,)])
<class 'list'>
py> type(x for x in (1,))
<class 'generator'>
So I think the similarity is complete. Further, if we think of "list
builder" as an abbreviation of "list builder syntax", we have:
- list builder syntax is syntax which returns a list;
- dict builder syntax is syntax which returns a dict;
- set builder syntax is syntax which returns a set;
- generator builder syntax is syntax which returns a generator.
Of course, there are other ways to build lists, such as calling the
constructor, or using a list display ("list literal", except it isn't
always a literal). But they're not *builder syntax* :-)
In hindsight, I think "spam builder (syntax)" would have been better
than the rather mysterious technical word "comprehension" and the not
very felicitous term "generator expression".
--
Steve
More information about the Python-Dev
mailing list