[Python-Dev] accumulator display syntax
Phillip J. Eby
pje at telecommunity.com
Thu Oct 16 10:50:44 EDT 2003
At 05:31 AM 10/16/03 -0700, Michael Chermside wrote:
>Alex Martelli writes:
> > I think we could extend indexing to mean something different when
> > the [ ] contain a 'for', just like we extended list display to mean
> > something different (list comprehension) when the [ ] contain a
> > 'for'. Syntax such as:
> >
> > Top(10)[ humor(joke) for joke in jokes ]
> >
> > does not suggest a list is _returned_, just like foo[23] doesn't.
>
>I find the syntax a bit confusing.
>
>Are we subscripting here, or are we juxtaposing one expression
>("Top(10)"), with a list comprehension ("[humor(joke) for joke in jokes]")?
>
>Not totally unreadable, but it rubs me the wrong way. I read [] used
>for subscripting as completely different from [] used for list literals
>and list comprehensions. They just happen to share the same pair of
>symbols. To me, this confuses the two somewhat.
I have to second on the syntax confusion, but for a different reason. This:
Top(10)[ humor(joke) for joke in jokes ]
Looks to me like some kind of *slice* syntax. I would read this as being
roughly equivalent to:
temp = Top(10)
[temp[humor(joke)] for joke in jokes ]
Top(10) and all the other accumulators proposed are, IMO, nothing more than
transformations of a sequence or iterator. Transformations are what
functions are for, and function syntax clearly expresses that the function
is being applied to the sequence or iterator, and returning a
result. Peter's syntax is too magical, and Alex's implies subscripting
that doesn't really exist. Both are misleading to a casual reader of the code.
More information about the Python-Dev
mailing list