PEP 289: Generator Expressions (please comment)
Werner Schiendl
n17999950.temp.werner at neverbox.com
Mon Oct 27 08:08:44 EST 2003
Hi,
Rainer Deyke wrote:
>
> At the very least, it would require parentheses if you want to create a list
> containing exactly one such generator.
>
> [(x for x in y)] # List containing generator
> [x for x in y] # List comprehension
>
>
personally the parentheses seem a little strange to me, or put another
way, I feel that simple parentheses around "everything" should not
change the meaning of the code.
For example, see current tuple syntax:
>>> def p(x):
... print type(x)
...
>>> p(5)
<type 'int'>
>>> p((5))
<type 'int'>
>>> p((5,))
<type 'tuple'>
As the code shows, just putting parentheses around the value 5 doesn't
make it a tuple.
Another point is that I think it's one of Python's advantages to not
require extensive parentheses e. g. for 'if' 'while' 'for' etc.
Unlike in C you can just write
if a > 7:
print "Large enough."
So requiring parentheses around the only item for a list with one
generator in it seems a little inconsistent in my eyes.
Maybe an alternative would be to have similar to tuples:
[x for x in y, ] # List containing generator
[x for x in y] # List comprehension
Just my 2c
Werner
More information about the Python-list
mailing list