48
votes

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
3
I find it especially interesting that for k, in d: print k does work in that case. - David Robinson
The Python grammar will show you what is allowed, for example, in an if statement. You'd have to read over the full grammar to figure out where unparenthesized tuples are accepted, which is why I'm not posting this as an answer. - chepner
...I fail to understand how you could have been surprised by the for failing with 0,... python identifiers must start with a (unicode)letter or underscore and 0 is not a valid python identifier hence I would have expected a SyntaxError there. - Bakuriu
That's why David said for k, not for 0, (It works i.e. it successfully does tuple unpacking into k for the case where the dict keys are 1-element tuples) - wim

3 Answers

47
votes

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_lists 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)
14
votes

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:

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.

0
votes

Parentheses are also required when you want to avoid ambiguity.

The following are two different expressions... just because something is an 'expression list', doesn't result in the expression list you might expect :)

(1, 2, 3) + (4, 5) # results in (1, 2, 3, 4, 5) because + does sequence.extend on the tuples
1, 2, 3 + 4, 5     # this results in (1, 2, 7, 5) because + adds the elements, since there were no parentheses to protect the separate tuples