@@ -216,13 +216,17 @@ string literals::
216216 Template(strings=('Hello', '!'), interpolations=(...))
217217
218218
219+ .. index ::
220+ single: parenthesized form
221+ single: () (parentheses)
222+
219223.. _parenthesized :
220224
221225Parenthesized groups
222226--------------------
223227
224- A :dfn: `group ` is an expression enclosed in parentheses.
225- The parenthesized group evaluates to the same value as the expression inside.
228+ A :dfn: `parenthesized group ` is an expression enclosed in parentheses.
229+ The group evaluates to the same value as the expression inside.
226230
227231Groups are used to override or clarify
228232:ref: `operator precedence <operator-precedence >`,
@@ -240,9 +244,15 @@ For example::
240244 12
241245 >>> 3 << (2 | 4) # Override precedence of the bitwise OR
242246 192
243- >>> (3 << 2) | 4 # Same as without parentheses, but much clearer
247+ >>> (3 << 2) | 4 # Same as without parentheses ( but much clearer)
244248 12
245249
250+ Note that not everything in parentheses is a *group *.
251+ Specifically, a parenthesized group must include exactly one expression,
252+ and cannot end with a comma.
253+ See :ref: `tuple displays <tuple-display >` and
254+ :ref: `generator expressions <genexpr >` for other parenthesized forms.
255+
246256Formally, the syntax for groups is:
247257
248258.. grammar-snippet ::
@@ -251,42 +261,105 @@ Formally, the syntax for groups is:
251261 group: '(' `assignment_expression ` ')'
252262
253263
264+ .. index ::
265+ single: tuple display
266+
267+ .. _tuple-display :
254268
255269Tuple displays
256270--------------
257271
258- ..
272+ A :dfn: `tuple display ` is a parenthesized expression that evaluates to a
273+ :class: `tuple ` object.
274+
275+ In the most common form, the parentheses contain two or more comma-separated
276+ expressions::
277+
278+ >>> (1, 2)
279+ (1, 2)
280+ >>> ('one', 'two', 'thr' + 'ee')
281+ ('one', 'two', 'three')
282+
283+ The expressions may be followed by an additional comma, which has no effect.
284+ (The trailing comma is often used for tuple displays that span multiple lines,
285+ so when a new entry is later added at the end, the existing line does not
286+ need to be modified)::
287+
288+ >>> (1, 2,)
289+ (1, 2)
290+ >>> (
291+ ... 'one',
292+ ... 'two',
293+ ... 'thr' + 'ee',
294+ ... )
295+ ('one', 'two', 'three')
259296
260- Parenthesized forms
261- -------------------
297+ At runtime, evaluating a tuple display results in a tuple that contains
298+ the results of the expressions, in order.
299+ Since tuples are immutable, the same rules as for literals apply: two
300+ occurrences of tuples with the `same values ` may or may not yield the same object.
262301
263- .. index ::
264- single: parenthesized form
265- single: () (parentheses); tuple display
302+ ... TODO:: Link `same values ` to "Literals and object identity" from the previous PR
266303
267- A parenthesized form is an optional expression list enclosed in parentheses:
304+ A tuple display may also contain a *single * expression.
305+ In this case, the trailing comma is mandatory -- without it, you get a
306+ :ref: `parenthesized group <parenthesized >`::
268307
269- .. productionlist :: python-grammar
270- parenth_form: "(" [` starred_expression `] ")"
308+ >>> ('single',)
309+ ('single',)
271310
272- A parenthesized expression list yields whatever that expression list yields: if
273- the list contains at least one comma, it yields a tuple; otherwise, it yields
274- the single expression that makes up the expression list.
311+ .. index :: pair: empty; tuple
275312
276- .. index :: pair: empty; tuple
313+ A tuple display may also contain *zero * expressions:
314+ empty parentheses denote the empty tuple.
315+ A trailing comma is *not * allowed in this case.
277316
278- An empty pair of parentheses yields an empty tuple object. Since tuples are
279- immutable, the same rules as for literals apply (i.e., two occurrences of the empty
280- tuple may or may not yield the same object).
317+ .. code-block ::
281318
282- .. index ::
283- single: comma
284- single: , (comma)
319+ >>> ()
320+ ()
285321
286- Note that tuples are not formed by the parentheses, but rather by use of the
287- comma. The exception is the empty tuple, for which parentheses *are *
288- required --- allowing unparenthesized "nothing" in expressions would cause
289- ambiguities and allow common typos to pass uncaught.
322+ To put it in other words, a tuple display is a parenthesized list of either:
323+
324+ - two or more comma-separated expressions, or
325+ - zero or more expressions, each followed by a comma.
326+
327+ The formal grammar for tuple expressions is:
328+
329+ .. grammar-snippet ::
330+ :group: python-grammar
331+
332+ tuple:
333+ | '(' `flexible_expression` (',' `flexible_expression`)+ [','] ')'
334+ | '(' `flexible_expression` ',' ')'
335+ | '(' ')'
336+
337+ .. note ::
338+
339+ .. index ::
340+ single: comma
341+ single: , (comma)
342+
343+ Note that tuple displays are not the only way to form tuples.
344+ In several places, Python's syntax allows forming a tuple without
345+ parentheses, only with a comma-separated list of expressions.
346+ The most prominent example is the ``return `` statement::
347+
348+ >>> def gimme_a_tuple():
349+ ... return 1, 2, 3
350+ ...
351+ >>> gimme_a_tuple()
352+ (1, 2, 3)
353+
354+ .. note to contributors:
355+ Another prominent example is the expression statement,
356+ but as of this writing, its docs imply that you need parentheses there.
357+ The example can be added after the documented grammar is fixed.
358+ This is tracked, broadly, in gh-127833.
359+
360+ These are not considered *tuple displays *, but follow similar rules.
361+ The use of a comma forms a tuple; without a comma, these forms evaluate
362+ to a single expression.
290363
291364
292365.. _comprehensions :
0 commit comments