Proposed new syntax

Rustom Mody rustompmody at gmail.com
Mon Aug 14 07:03:49 EDT 2017


On Monday, August 14, 2017 at 3:30:39 PM UTC+5:30, Ben Finney wrote:
> Steven D'Aprano writes:
> 
> > On Sun, 13 Aug 2017 21:06:08 -0700, Rustom Mody wrote:
> >
> > > Here's a bunch of different ways in which a mapping comprehension
> > > could be implemented:
> >
> > Not in Python they couldn't be
> 
> You began by asking what people would expect syntax to mean.
> 
> Then you expressed surprise that anyone would think a comprehension
> would be interpreted by the reader as a single operation.
> 
> The designer of that feature expressed that yes, the intention was that
> it be interpreted as a single conceptual operation, not a looping
> sequence of operations.
> 
> Your latest counter has been that Python means something special, beyond
> what the feature's designer intended, and it's implemented as a looping
> sequence of operations.
> 
> So it's normal to point out that Python's implementation is just one way
> that it could be implemented, hence the reader can reasonably expect
> that it's not the way Python implemented it.
> 
> You were apparently, at the start of this thread, honestly seeking to
> know how people interpret the syntax.
> 
> At what point will you accept the feedback: That the comprehension
> syntax *does not* necessarily connote a procedural loop, but instead can
> quite reasonably be interpreted as its designer intended, a single
> conceptual operation.

😃

I thought that this – Steven's OP – was some joke or prank question.
As best as I could make out all the initial responders giving some supposed 
meaning to the various 'comprehensions' seemed to be playing along
Since I had nothing cute to say… remained quiet

Then in quick succession Terry, Cecil, yourself gave responses that I would give
in seriousness if anything, better than what I could come up with
So once again I had nothing to add

I just put in some history to say that yes, you can take the expansion
of comprehensions as loops-with-appends given in the python docs as God's Word

For myself, if the python docs contradict the last 100 years or prior art/math history,
the latter takes precedence 

All I want to say is the gratuitous incidental resemblance of for-loops
with comprehensions confuses not just beginners but even the python implementers:

This leak

>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> x
4
>>> 

got corrected from python2 to 3
>>> [x for x in range(5)]
[0, 1, 2, 3, 4]
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

However this leak still remains:

>>> fl = [(lambda x : x + i) for i in range(5)]
>>> [f(2) for f in fl] 
[6, 6, 6, 6, 6]

Compare to haskell:

Prelude> let fl = [(\ x -> x + i) | i <- [0..4]]
Prelude> [f 2 | f <- fl]
[2,3,4,5,6]
Prelude> 

I strongly suspect this bugginess to be related to Greg's original idea of using
that for-loop-containing-append as an informal off-the-cuff explanation becoming 
the formal definition


[And I am still unsure whether this OP is a prank trick-question… ]



More information about the Python-list mailing list