Question on style.

Alex Martelli aleaxit at yahoo.com
Fri Sep 15 17:14:29 CEST 2000

```"Paul Duffin" <pduffin at hursley.ibm.com> wrote in message
news:39C1E4FA.CFE134A6 at hursley.ibm.com...
> Remco Gerlich wrote:
> >
> > Paul Duffin wrote in comp.lang.python:
> > > Any one like to comment on why this particular order was chosen over
> > > the more 'readable' one of for first then expression.
> >
> > I *think* this syntax was adopted from functional languages,
> > for instance, in Haskell the "list of squares of the numbers 1 to 100"
is
> >     [ n*n | n <- [1..100] ]
> > Which becomes the slightly more verbose
> >     [ n*n for n in range(1,101) ]
>
> The single usage is quite readable but when you have multiple uses in
> the same expression then it becomes more difficult to read.

Multiple uses of what, the identifier coming from the generator?  You
do here -- n*n is two uses of the identifier n.

> How should the example from the original poster be 'read' ?
>
> frames = [[score[frame] for score in scores] \
>           for frame in range(3)]

"The list of lists of score of frame, for score in scores, for frame in
range
of three".

It's important that the reading starts with what you're listing ("the list
of"), just as in math you start with "the set of" (whatever).

> What would a similar mathematical expression be ?

Using 'in' for the epsilon-like sign of element-of:

{ { func(x) | func in afamily } | x in {0, 1, 2} }

"The set of sets of func of x, with func in afamily, with x in zero, one,
two".

Note the difference with:

"The set of func of x, with func in afamily and x in zero, one, two", i.e.

{ func(x) | func in afamily, x in {0, 1, 2} }

equivalent to Python's:

[ score[frame] for score in scores for frame in range(3) ]

"The list of score of frame, for score in scores, for frame in range of
three".

> > I'd pronounce this "the list of squares of the numbers 1 to 100".
>
> Surely the mathematical interpretation is
>     "the set of squares of the numbers 1 to 100"
>          ^^^
> lists are not a mathematical concept are they ? IANAM.

They are, in a sense (the whole theory of types is on a pretty good
mathematical basis nowadays), but what the original poster intended
(judging from context) was indeed surely "set", not "list".

Alex

```