Re: comprehension abbreviation (was: Adding any() and all())

Gareth McCaughan wrote:
Some bit of my brain is convinced that [x in stuff if condition] is the Right Syntax and keeps making me type it even though I know it doesn't work.
(and I agree with Gareth) On Monday 2005-03-14 12:42, Eric Nieuwland wrote:
The full syntax is: [ f(x) for x in seq if pred(x) ] being allowed to write 'x' instead of 'identity(x)' is already a shortcut, just as dropping the conditional part.
I think this is the heart of the disagreement. Mentally, I'm not collecting some function of x (which happens to be identity). I am filtering an existing set. Being able to collect f(x) instead is just a useful but hackish shortcut. Gareth again:
and in fact no set theorist would be at all troubled by seeing
{ x in S : predicate(x) }
which is the nearest equivalent in mathematical notation for the abbreviated comprehension expressions being discussed.
Again, I agree. I think that is what I am unconsciously writing, by translating the ":" into "if" -jJ

Jim Jewett wrote:
Gareth McCaughan wrote:
Some bit of my brain is convinced that [x in stuff if condition] is the Right Syntax and keeps making me type it even though I know it doesn't work.
(and I agree with Gareth)
On Monday 2005-03-14 12:42, Eric Nieuwland wrote:
The full syntax is: [ f(x) for x in seq if pred(x) ] being allowed to write 'x' instead of 'identity(x)' is already a shortcut, just as dropping the conditional part.
I think this is the heart of the disagreement.
Mentally, I'm not collecting some function of x (which happens to be identity). I am filtering an existing set. Being able to collect f(x) instead is just a useful but hackish shortcut.
Have it your own way, but if you happen to need a list of transformed elements of a filtered list (and that isn't an uncommon requirement) then the idea of selecting the set members and then transforming the copies as a separate step seems a little ... unnecessary. Having to write [x for x in seq] to produce a copy of a list doesn't seem that outrageous to me, and I don't find the predicate-less case of your proposal that convincing: [x in seq] seems somehow too terse. [...] regards Steve -- Steve Holden +1 703 861 4237 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Python Web Programming http://pydish.holdenweb.com/

Steve Holden <steve@holdenweb.com> wrote: [...]
Having to write
[x for x in seq]
to produce a copy of a list doesn't seem that outrageous to me, and I don't find the predicate-less case of your proposal that convincing:
[x in seq]
seems somehow too terse.
And is already valid Python syntax; producing a list of a boolean (if x is bound), a TypeError (if seq is a dictionary, x is bound, and x isn't hashable), or a NameError (if x is not bound). If I recall, changing the meaning of valid Python syntax is to be frowned upon, and the suggestion should be tossed out the window strictly because of that reason. As for "for x" or its equivalent, being too much additional overhead to type in list comprehensions, I think maybe we are getting too picky for our own good. - Josiah

I thought I had been clear already, but since this thread keeps going maybe I need to reiterate that there's zero chance that this syntax proposal (or anything like it) will be accepted. You all are of course free to continue to discuss it, but as I've explained before it just isn't worth it. -- --Guido van Rossum (home page: http://www.python.org/~guido/)

"Steve Holden" <steve@holdenweb.com> wrote in message news:d2asg9$3tf$1@sea.gmane.org...
Having to write
[x for x in seq]
to produce a copy of a list doesn't seem that outrageous to me,
Except for (currently) leaving the last value of sequence bound to 'x' after making the copy, how is the above different from list(seq)? TJR

On Mar 29, 2005, at 17:41, Terry Reedy wrote: ...
"Steve Holden" <steve@holdenweb.com> wrote in message news:d2asg9$3tf$1@sea.gmane.org...
Having to write
[x for x in seq]
to produce a copy of a list doesn't seem that outrageous to me,
Except for (currently) leaving the last value of sequence bound to 'x' after making the copy, how is the above different from list(seq)?
Well, it's less concise, and over an order of magnitude slower: Nimue:~/pypy alex$ python2.4 -mtimeit -s'seq=range(1000)' '[x for x in seq]' 1000 loops, best of 3: 312 usec per loop Nimue:~/pypy alex$ python2.4 -mtimeit -s'seq=range(1000)' 'list(seq)' 10000 loops, best of 3: 24.3 usec per loop I fail to see any advantages to compensate for these minuses. Alex
participants (6)
-
Alex Martelli
-
Guido van Rossum
-
Jim Jewett
-
Josiah Carlson
-
Steve Holden
-
Terry Reedy