classification
Title: Restrict syntax for tuple literals with one element
Type: Stage: resolved
Components: Interpreter Core Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, brett.cannon, danijar, xtreak, yselivanov
Priority: normal Keywords:

Created on 2019-04-15 18:27 by danijar, last changed 2019-04-15 18:29 by brett.cannon. This issue is now closed.

Messages (2)
msg340298 - (view) Author: danijar (danijar) Date: 2019-04-15 18:27
A tuple can be created with or without parentheses:

a = (1, 2, 3)

a = 1, 2, 3

While both are intuitive in this example, omitting the parentheses can lead to hard to find errors when there is only one element:

a = (1,)

a = 1,

The first is clear but the second can easily occur as a typo when the programmer actually just wanted to assign an integer (comma is next to enter on many keyboards).

I think ideally, omitting parentheses in the single element case would throw a SyntaxError.

On the other hand, I assume that it could be difficult to separate the behavior or tuple creating with an without parentheses, since the parentheses are probably not actually part of the tuple literal.
msg340300 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-04-15 18:29
Thanks for the idea, but this would break way too much code to warrant changing it. Plus making this act differently just in the single-item case is too much special-casing/surprise.
History
Date User Action Args
2019-04-15 18:29:48brett.cannonsetstatus: open -> closed
nosy: brett.cannon, benjamin.peterson, yselivanov, xtreak, danijar
messages: + msg340300

resolution: rejected
stage: resolved
2019-04-15 18:27:31danijarcreate