This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: allow exprlist as the iterators of comprehensions to be consistent with for statements
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, serhiy.storchaka, thautwarm, yselivanov
Priority: normal Keywords:

Created on 2018-09-29 22:05 by thautwarm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg326692 - (view) Author: thautwarm (thautwarm) * Date: 2018-09-29 22:05
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?
msg326693 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-09-29 22:15
This is intentional. We don't want people accidentally writing e.g.

[i for i in range(10), j for j in range(10)]
msg326694 - (view) Author: thautwarm (thautwarm) * Date: 2018-09-29 22:26
Well, sorry for that.
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79026
2018-09-29 22:26:38thautwarmsetmessages: + msg326694
2018-09-29 22:15:19gvanrossumsetstatus: open -> closed
resolution: rejected
messages: + msg326693

stage: resolved
2018-09-29 22:06:30thautwarmsettype: behavior
components: + Interpreter Core
versions: + Python 3.8
2018-09-29 22:05:23thautwarmcreate