Skip to content

Commit bfef3fa

Browse files
committed
Merge branch 'expressions-parenthesized' into expressions-lists-sets-dicts
2 parents 12828e5 + a703113 commit bfef3fa

1 file changed

Lines changed: 179 additions & 39 deletions

File tree

Doc/reference/expressions.rst

Lines changed: 179 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,40 @@ Atoms
4848
.. index:: atom
4949

5050
Atoms are the most basic elements of expressions.
51-
The simplest atoms are :ref:`names <identifiers>` or literals.
52-
Forms enclosed in parentheses, brackets or braces are also categorized
53-
syntactically as atoms.
51+
The simplest atoms are :ref:`builtin constants <atom-singletons>`,
52+
:ref:`names <identifiers>` and :ref:`literals <atom-literals>`.
53+
More complex atoms are enclosed in paired delimiters:
54+
55+
- ``()`` (parentheses): :ref:`groups <parenthesized>`,
56+
:ref:`tuple displays <tuple-display>`,
57+
:ref:`yield atoms <yieldexpr>`, and
58+
:ref:`generator expressions <genexpr>`;
59+
- ``[]`` (square brackets): :ref:`list displays <lists>`;
60+
- ``{}`` (curly braces): :ref:`dictionary <dict>` and :ref:`set <set>` displays.
5461

5562
Formally, the syntax for atoms is:
5663

5764
.. grammar-snippet::
5865
:group: python-grammar
5966

6067
atom:
68+
| `builtin_constant`
69+
| `identifier`
70+
| `literal`
71+
| `enclosure`
72+
builtin_constant:
6173
| 'True'
6274
| 'False'
6375
| 'None'
6476
| '...'
65-
| `identifier`
66-
| `literal`
67-
| `enclosure`
6877
enclosure:
69-
| `parenth_form`
78+
| `group`
79+
| `tuple`
80+
| `yield_atom`
81+
| `generator_expression`
7082
| `list_display`
7183
| `dict_display`
7284
| `set_display`
73-
| `generator_expression`
74-
| `yield_atom`
7585
7686

7787
.. _atom-singletons:
@@ -201,6 +211,7 @@ The formal grammar for literals is:
201211

202212
literal: `strings` | `NUMBER`
203213

214+
.. _literals-identity:
204215

205216
.. index::
206217
triple: immutable; data; type
@@ -309,38 +320,151 @@ Formally:
309320
strings: (`STRING` | `fstring`)+ | `tstring`+
310321

311322

323+
.. index::
324+
single: parenthesized form
325+
single: () (parentheses)
326+
312327
.. _parenthesized:
313328

314-
Parenthesized forms
315-
-------------------
329+
Parenthesized groups
330+
--------------------
316331

317-
.. index::
318-
single: parenthesized form
319-
single: () (parentheses); tuple display
332+
A :dfn:`parenthesized group` is an expression enclosed in parentheses.
333+
The group evaluates to the same value as the expression inside.
320334

321-
A parenthesized form is an optional expression list enclosed in parentheses:
335+
Groups are used to override or clarify
336+
:ref:`operator precedence <operator-precedence>`,
337+
in the same way as in math notation.
338+
For example::
322339

323-
.. productionlist:: python-grammar
324-
parenth_form: "(" [`starred_expression`] ")"
340+
>>> 3 << 2 | 4
341+
12
342+
>>> 3 << (2 | 4) # Override precedence of the | (bitwise OR)
343+
192
344+
>>> (3 << 2) | 4 # Same as without parentheses (but more clear)
345+
12
325346

326-
A parenthesized expression list yields whatever that expression list yields: if
327-
the list contains at least one comma, it yields a tuple; otherwise, it yields
328-
the single expression that makes up the expression list.
347+
Note that not everything in parentheses is a *group*.
348+
Specifically, a parenthesized group must include exactly one expression,
349+
and cannot end with a comma.
350+
See :ref:`tuple displays <tuple-display>` and
351+
:ref:`generator expressions <genexpr>` for other parenthesized forms.
329352

330-
.. index:: pair: empty; tuple
353+
Formally, the syntax for groups is:
354+
355+
.. grammar-snippet::
356+
:group: python-grammar
357+
358+
group: '(' `assignment_expression` ')'
331359

332-
An empty pair of parentheses yields an empty tuple object. Since tuples are
333-
immutable, the same rules as for literals apply (i.e., two occurrences of the empty
334-
tuple may or may not yield the same object).
335360

336361
.. index::
362+
single: tuple display
337363
single: comma
338364
single: , (comma)
339365

340-
Note that tuples are not formed by the parentheses, but rather by use of the
341-
comma. The exception is the empty tuple, for which parentheses *are*
342-
required --- allowing unparenthesized "nothing" in expressions would cause
343-
ambiguities and allow common typos to pass uncaught.
366+
.. _tuple-display:
367+
368+
Tuple displays
369+
--------------
370+
371+
A :dfn:`tuple display` is a parenthesized expression that evaluates to a
372+
:class:`tuple` object.
373+
374+
In the most common form, the parentheses contain two or more comma-separated
375+
expressions::
376+
377+
>>> (1, 2)
378+
(1, 2)
379+
>>> ('one', 'two', 'thr' + 'ee')
380+
('one', 'two', 'three')
381+
382+
The expressions may be followed by an additional comma, which has no effect::
383+
384+
>>> (1, 2,)
385+
(1, 2)
386+
387+
.. note::
388+
389+
The trailing comma is often used for tuple displays that span multiple lines
390+
(using :ref:`implicit line joining <implicit-joining>`),
391+
so when a new entry is later added at the end, the existing line does not
392+
need to be modified::
393+
394+
>>> (
395+
... 'one',
396+
... 'two',
397+
... 'three',
398+
... )
399+
('one', 'two', 'three')
400+
401+
At runtime, evaluating a tuple display results in a tuple that contains
402+
the results of the expressions, in order.
403+
Since tuples are immutable, :ref:`object identity rules for literals <literals-identity>`
404+
also apply to tuples: two occurrences of tuples with the same values may
405+
or may not yield the same object.
406+
407+
A tuple display may also contain a *single* expression.
408+
In this case, the trailing comma is mandatory -- without it, you get a
409+
:ref:`parenthesized group <parenthesized>`::
410+
411+
>>> ('single',) # single-element tuple
412+
('single',)
413+
>>> ('single') # no comma: single string
414+
'single'
415+
416+
.. index:: pair: empty; tuple
417+
418+
A tuple display may also contain *zero* expressions:
419+
empty parentheses denote the empty tuple.
420+
A trailing comma is *not* allowed in this case.
421+
422+
.. code-block::
423+
424+
>>> ()
425+
()
426+
427+
To put it in other words, a tuple display is a parenthesized list of either:
428+
429+
- two or more comma-separated expressions, or
430+
- zero or more expressions, each followed by a comma.
431+
432+
.. note::
433+
434+
Python's syntax also includes :ref:`expression lists <exprlists>`,
435+
where a comma-separated list of expressions is *not* enclosed in parentheses
436+
but evaluates to tuple.
437+
438+
In other words, when it comes to tuple syntax, the comma is more important
439+
that the use of parentheses.
440+
Only the empty tuple is spelled without a comma.
441+
442+
.. index::
443+
pair: iterable; unpacking
444+
single: * (asterisk); in expression lists
445+
446+
Any expression in a tuple display may be prefixed with an asterisk (``*``).
447+
This denotes :ref:`iterable unpacking as in expression lists <iterable-unpacking>`:
448+
449+
450+
>>> numbers = (1, 2)
451+
>>> (*numbers, 'word', *numbers)
452+
(1, 2, 'word', 1, 2)
453+
454+
.. versionadded:: 3.5
455+
Iterable unpacking in tuple displays, originally proposed by :pep:`448`.
456+
457+
.. index:: pair: trailing; comma
458+
459+
The formal grammar for tuple expressions is:
460+
461+
.. grammar-snippet::
462+
:group: python-grammar
463+
464+
tuple:
465+
| '(' `flexible_expression` (',' `flexible_expression`)+ [','] ')'
466+
| '(' `flexible_expression` ',' ')'
467+
| '(' ')'
344468
345469

346470
.. _comprehensions:
@@ -2178,6 +2302,10 @@ functions created with lambda expressions cannot contain statements or
21782302
annotations.
21792303

21802304

2305+
.. index::
2306+
single: comma
2307+
single: , (comma)
2308+
21812309
.. _exprlists:
21822310

21832311
Expression lists
@@ -2202,12 +2330,32 @@ containing at least one comma yields a tuple. The length of
22022330
the tuple is the number of expressions in the list. The expressions are
22032331
evaluated from left to right.
22042332

2333+
.. index:: pair: trailing; comma
2334+
2335+
A trailing comma is required only to create a one-item tuple,
2336+
such as ``1,``; it is optional in all other cases.
2337+
A single expression without a
2338+
trailing comma doesn't create a tuple, but rather yields the value of that
2339+
expression. (To create an empty tuple, use an empty pair of parentheses:
2340+
``()``.)
2341+
2342+
2343+
.. _iterable-unpacking:
2344+
22052345
.. index::
22062346
pair: iterable; unpacking
22072347
single: * (asterisk); in expression lists
22082348

2209-
An asterisk ``*`` denotes :dfn:`iterable unpacking`. Its operand must be
2210-
an :term:`iterable`. The iterable is expanded into a sequence of items,
2349+
Iterable unpacking
2350+
------------------
2351+
2352+
In an expression list or tuple, list or set display, any expression
2353+
may be prefixed with an asterisk (``*``).
2354+
This denotes :dfn:`iterable unpacking`.
2355+
2356+
At runtime, the asterisk-prefixed expression must evaluate
2357+
to an :term:`iterable`.
2358+
The iterable is expanded into a sequence of items,
22112359
which are included in the new tuple, list, or set, at the site of
22122360
the unpacking.
22132361

@@ -2217,15 +2365,6 @@ the unpacking.
22172365
.. versionadded:: 3.11
22182366
Any item in an expression list may be starred. See :pep:`646`.
22192367

2220-
.. index:: pair: trailing; comma
2221-
2222-
A trailing comma is required only to create a one-item tuple,
2223-
such as ``1,``; it is optional in all other cases.
2224-
A single expression without a
2225-
trailing comma doesn't create a tuple, but rather yields the value of that
2226-
expression. (To create an empty tuple, use an empty pair of parentheses:
2227-
``()``.)
2228-
22292368

22302369
.. _evalorder:
22312370

@@ -2249,6 +2388,7 @@ their suffixes::
22492388

22502389

22512390
.. _operator-summary:
2391+
.. _operator-precedence:
22522392

22532393
Operator precedence
22542394
===================

0 commit comments

Comments
 (0)