Message326692
Currently we can use `exprlist` as the iterator in a `for`
statement:
```
for i in 1, 2,:
print(i)
1
2
```
However, when it comes to comprehension expressions:
```
[i for i in 1, 2]
SyntaxError: invalid syntax
```
I know there might be some reason that leads to the absence of the consistency between `for` expression and statement, but IMO until now it could be better to allow `exprlist` to be the iterator of comprehensions. I'm not sure whether our community has noticed this problem so I'm to propose it here.
A crucial benefit from this change is that we can avoid the ambiguity when a comprehension is accepted as function parameter.
```
f(for i in [1, 2, 3], 1, 2)
```
We now get a SyntaxError when writing such codes, but people who know this syntax might think the parameters can be distinguished from each other, but it's still not allowed.
Allowing `exprlist` to be the iterator slot of a comprehension would be a rational solution. If `f(for i in [1, 2, 3], 1, 2)` is equivalent to `f(for i ([1, 2, 3], 1, 2))`, it will be natural to restrict the number of parameters to be just 1 when the parameter is a comprehension.
You can slightly accept this workaround and try following examples:
```
f(for i in 1,)
f(for i in 1, for j in 2, 3,)
f(for i in 1, 2 if cond(i) for j in 3, for k in 4,)
```
Obviously, in each of above cases, the number of parameters is 1,
just because a `exprlist` could the iterator of a comprehension.
The disadvantage of this change is, there is not too many related use cases, for any `[expr for target in iter_elem1, iter_elem2, ...]` could be altered as `[expr for target in (iter_elem1, iter_elem2, ...)]`. Meanwhile, that might not be true when it comes to someone works frequently with interactive python.
Finally, I have finished this implementation at https://github.com/thautwarm/cpython/tree/exprlist-in-comprehensions, and I do want to make contributions to cpython projects. If most of you feel comfortable with this change, may I make a PR next? |
|
Date |
User |
Action |
Args |
2018-09-29 22:05:23 | thautwarm | set | recipients:
+ thautwarm, gvanrossum, serhiy.storchaka, yselivanov |
2018-09-29 22:05:23 | thautwarm | set | messageid: <1538258723.92.0.545547206417.issue34845@psf.upfronthosting.co.za> |
2018-09-29 22:05:23 | thautwarm | link | issue34845 messages |
2018-09-29 22:05:23 | thautwarm | create | |
|