@@ -48,25 +48,37 @@ Atoms
4848.. index :: atom
4949
5050Atoms 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
5562Formally, 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- | (`group` | `tuple` | `yield_atom` | `generator_expression`) # in (parentheses)
78+ | `group`
79+ | `tuple`
80+ | `yield_atom`
81+ | `generator_expression`
7082 | `list_display`
7183 | `dict_display`
7284 | `set_display`
@@ -199,6 +211,7 @@ The formal grammar for literals is:
199211
200212 literal: `strings ` | `NUMBER `
201213
214+ .. _literals-identity :
202215
203216.. index ::
204217 triple: immutable; data; type
@@ -324,18 +337,11 @@ Groups are used to override or clarify
324337in the same way as in math notation.
325338For example::
326339
327- >>> 3 + 2 * 4
328- 11
329- >>> (3 + 2) * 4 # Override precedence of the addition
330- 20
331- >>> 3 + (2 * 4) # Same effect as without parentheses
332- 11
333-
334340 >>> 3 << 2 | 4
335341 12
336- >>> 3 << (2 | 4) # Override precedence of the bitwise OR
342+ >>> 3 << (2 | 4) # Override precedence of the | ( bitwise OR)
337343 192
338- >>> (3 << 2) | 4 # Same as without parentheses (but much clearer )
344+ >>> (3 << 2) | 4 # Same as without parentheses (but more clear )
339345 12
340346
341347Note that not everything in parentheses is a *group *.
@@ -354,6 +360,8 @@ Formally, the syntax for groups is:
354360
355361.. index ::
356362 single: tuple display
363+ single: comma
364+ single: , (comma)
357365
358366.. _tuple-display :
359367
@@ -371,33 +379,39 @@ expressions::
371379 >>> ('one', 'two', 'thr' + 'ee')
372380 ('one', 'two', 'three')
373381
374- The expressions may be followed by an additional comma, which has no effect.
375- (The trailing comma is often used for tuple displays that span multiple lines,
376- so when a new entry is later added at the end, the existing line does not
377- need to be modified)::
382+ The expressions may be followed by an additional comma, which has no effect::
378383
379384 >>> (1, 2,)
380385 (1, 2)
381- >>> (
382- ... 'one',
383- ... 'two',
384- ... 'thr' + 'ee',
385- ... )
386- ('one', 'two', 'three')
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')
387400
388401At runtime, evaluating a tuple display results in a tuple that contains
389402the results of the expressions, in order.
390- Since tuples are immutable, the same rules as for literals apply: two
391- occurrences of tuples with the `same values ` may or may not yield the same object.
392-
393- ... TODO:: Link `same values ` to "Literals and object identity" from the previous PR
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.
394406
395407A tuple display may also contain a *single * expression.
396408In this case, the trailing comma is mandatory -- without it, you get a
397409:ref: `parenthesized group <parenthesized >`::
398410
399- >>> ('single',)
411+ >>> ('single',) # single-element tuple
400412 ('single',)
413+ >>> ('single') # no comma: single string
414+ 'single'
401415
402416.. index :: pair: empty; tuple
403417
@@ -415,6 +429,33 @@ To put it in other words, a tuple display is a parenthesized list of either:
415429- two or more comma-separated expressions, or
416430- zero or more expressions, each followed by a comma.
417431
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+
418459The formal grammar for tuple expressions is:
419460
420461.. grammar-snippet ::
@@ -425,33 +466,6 @@ The formal grammar for tuple expressions is:
425466 | '(' `flexible_expression` ',' ')'
426467 | '(' ')'
427468
428- .. note ::
429-
430- .. index ::
431- single: comma
432- single: , (comma)
433-
434- Note that tuple displays are not the only way to form tuples.
435- In several places, Python's syntax allows forming a tuple without
436- parentheses, only with a comma-separated list of expressions.
437- The most prominent example is the ``return `` statement::
438-
439- >>> def gimme_a_tuple():
440- ... return 1, 2, 3
441- ...
442- >>> gimme_a_tuple()
443- (1, 2, 3)
444-
445- .. note to contributors:
446- Another prominent example is the expression statement,
447- but as of this writing, its docs imply that you need parentheses there.
448- The example can be added after the documented grammar is fixed.
449- This is tracked, broadly, in gh-127833.
450-
451- These are not considered *tuple displays *, but follow similar rules.
452- The use of a comma forms a tuple; without a comma, these forms evaluate
453- to a single expression.
454-
455469
456470.. _comprehensions :
457471
@@ -2288,6 +2302,10 @@ functions created with lambda expressions cannot contain statements or
22882302annotations.
22892303
22902304
2305+ .. index ::
2306+ single: comma
2307+ single: , (comma)
2308+
22912309.. _exprlists :
22922310
22932311Expression lists
@@ -2312,12 +2330,32 @@ containing at least one comma yields a tuple. The length of
23122330the tuple is the number of expressions in the list. The expressions are
23132331evaluated from left to right.
23142332
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+
23152345.. index ::
23162346 pair: iterable; unpacking
23172347 single: * (asterisk); in expression lists
23182348
2319- An asterisk ``* `` denotes :dfn: `iterable unpacking `. Its operand must be
2320- 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,
23212359which are included in the new tuple, list, or set, at the site of
23222360the unpacking.
23232361
@@ -2327,15 +2365,6 @@ the unpacking.
23272365.. versionadded :: 3.11
23282366 Any item in an expression list may be starred. See :pep: `646 `.
23292367
2330- .. index :: pair: trailing; comma
2331-
2332- A trailing comma is required only to create a one-item tuple,
2333- such as ``1, ``; it is optional in all other cases.
2334- A single expression without a
2335- trailing comma doesn't create a tuple, but rather yields the value of that
2336- expression. (To create an empty tuple, use an empty pair of parentheses:
2337- ``() ``.)
2338-
23392368
23402369.. _evalorder :
23412370
0 commit comments