[Python-Dev] comprehension abbreviation (was: Adding any() and all())

Guido van Rossum gvanrossum at gmail.com
Sun Mar 13 17:26:10 CET 2005

[Nick Coghlan]
> > That 'x in seq' bit still shouts "containment" to me rather than
> > iteration, though.
> >
> > Perhaps repurposing 'from':
> >
> >   (x from seq if f(x))
> >
> > That rather breaks TOOWTDI though (since it is essentially new syntax
> > for a for loop). And I have other hopes for the meaning of (x from ()). . .

[Greg Ewing]
> How about:
>    (for x in seq if f(x))
> It still has the word 'for' in it and avoids mentioning
> x more times than necessary.
> Although I can't help feeling that it should be some
> other word instead, such as
>    (all x in seq if f(x))
> or
>    (every x in seq if f(x))

I doubt that we'll find a satisfactory solution for this issue. Here's why:

- We're only talking of a savings of 4 characters (plus two spaces) in
the best case, assuming the identifier can be a single letter (it's
scope is only the current expression anyway).

- Ping's proposal ('x in seq if f(x)') IMO loses for several reasons:
it would be a hugely ambiguous use of 'in', and would cut off a
possible future syntax for conditional expression(*): 'A if T else B'.

- The various improvements on Ping's proposal reduce the amount of
typing saved even more ('every' is actually longer than 'for x').

- Before anybody asks, I really do think the reason this is requested
at all is really just to save typing; there isn't the "avoid double
evaluation" argument that helped acceptance for assignment operators
(+= etc.), and I find redability is actually improved with 'for'.

(*) Pleas stop calling it 'ternary expression'. That doesn't explain
what it means. It's as if we were to refer to the + operator as
'binary expression'.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list