[Python-Dev] Python Language Reference has no mention of list comÃprehensions

MRAB python at mrabarnett.plus.com
Thu Dec 3 21:43:26 EST 2015


On 2015-12-04 01:56, Chris Angelico wrote:
> On Fri, Dec 4, 2015 at 12:25 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> I don't see any good reason for maintaining that there's just one
>> syntax, "display", which comes in two forms: a comma-separated set of
>> values, or a for-loop. The only thing they have in common (syntax-wise)
>> is that they both use [ ] as delimiters. They look different, they
>> behave differently, and only one matches what the list actually displays
>> as. Why use one term for what is clearly two distinct (if related)
>> syntaxes?
>
> You come across something syntactic that begins by opening a square
> bracket, and you know that its semantics are: "construct a new list".
> That's what's common here.
>
> What goes *inside* those brackets can be one of two things:
>
> 1) A (possibly empty) comma-separated sequence of expressions
>
> 2) One or more nested 'for' loops, possibly guarded by 'if's, and a
> single expression
>
> So we have two subforms of the same basic syntax. The first one
> corresponds better to the output format, in the same way that a string
> literal might correspond to its repr under specific circumstances.
> Neither is a literal. Neither is a call to a constructor function
> (contrast "list()" or "list.__new__(list)", which do call a
> constructor). So what is this shared syntax? Whatever word is used,
> it's going to be a bit wrong. I'd be happy with either "constructor"
> or "display", myself.
>
The problem with "constructor" is that it's already used for the "__new__"
class method.



More information about the Python-Dev mailing list