yet another list compr. suggestion

P

palo

I appologize for suggesting (though very humbly) a syntax extension
(maybe particularly idiotic) but just as a wild fantasy, what would you
think about this:
as a substitute for filter, people often use
[x for x in y if z(x)]
The suggestion is that the same result could be achieved by
[x in y if z(x)]
 
A

Alan Franzoni

palo on comp.lang.python said:
The suggestion is that the same result could be achieved by
[x in y if z(x)]

It's just a special case... and it saves very few carachters... I don't
think it would justify an update to the parser. What if you want to do
something like:

[str(x) for x in y if z(x)]

You've got to stick to the present syntax.

--
Alan Franzoni <[email protected]>
-
Togli .xyz dalla mia email per contattarmi.
Rremove .xyz from my address in order to contact me.
-
GPG Key Fingerprint:
5C77 9DC3 BD5B 3A28 E7BC 921A 0255 42AA FE06 8F3E
 
P

palo

you'd save more characters in
[not_very_convenient_name for not_very_convenient_name in y if z]
(just kidding)
 
P

palo

you'd save more in

[not_very_convenient_name for not_very_convenient_name in y if z]

(just kidding)
 
C

Christoph Zwerschke

palo said:
> ... what would you think about this:
> as a substitute for filter, people often use
> [x for x in y if z(x)]
> The suggestion is that the same result could be achieved by
> [x in y if z(x)]

In the original syntax, if z(x) is always true,
you can leave the "if z(x)" part away, which is intuitive.
But in your syntax, the "if z(x)" is essential.
If you leave it away, you get a completely different
construct, a list with a bool value as element.
You would not expect that an "if" keyword indicates a loop.

I think the "for" is absolutely essential to recognize
a list comprehension, not only for the parser, but also
for the human reader. It's already confusing enough.
Just compare the following:

[x in y] -> list with one element
[x in y if z(x)] -> list comprehension (in your syntax)
[x in y if z(x) else 0] -> list with one element (Py 2.5)

-- Christoph
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top