When are parentheses required around a tuple?
Is there a reference somewhere defining precisely when enclosing tuples with parentheses is or is not required?
Here is an example that surprised me recently:
>>> d = {}
>>> d[0,] = 'potato'
>>> if 0, in d:
File "<stdin>", line 1
if 0, in d:
^
SyntaxError: invalid syntax
Solution 1:
The combining of expressions to create a tuple using the comma token is termed an expression_list
. The rules of operator precedence do not cover expression lists; this is because expression lists are not themselves expressions; they become expressions when enclosed in parentheses.
So, an unenclosed expression_list
is allowed anywhere in Python that it is specifically allowed by the language grammar, but not where an expression
as such is required.
For example, the grammar of the if statement is as follows:
if_stmt ::= "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]
Because the production expression
is referenced, unenclosed expression_list
s are not allowed as the subject of the if
statement. However, the for statement accepts an expression_list
:
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
So the following is allowed:
for x in 1, 2, 3:
print(x)
Solution 2:
Anywhere you are allowed to use the expression_list
term, you do not need to use parenthesis.
The if
statement requires an expression
, and does not support an expression_list
.
Examples of syntax that does allow expression_list
:
- The
return
statement yield
expressions- assignment (including augmented assignment)
- The
for
statement.
Grepping the Expressions, Simple and Compound statements documentation for expression_list
will tell you all locations that expression_list
is used in the Python grammar.