Skip to content

Commit 112d8ac

Browse files
encukoublaisepStanFromIreland
authored
gh-141984: Reword and reorganize the first part of Atoms docs (GH-144117)
Co-authored-by: Blaise Pabon <blaise@gmail.com> Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent 3f50432 commit 112d8ac

File tree

4 files changed

+167
-51
lines changed

4 files changed

+167
-51
lines changed

Doc/faq/programming.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,8 @@ to the object:
18521852
13891296
18531853

18541854

1855+
.. _faq-identity-with-is:
1856+
18551857
When can I rely on identity tests with the *is* operator?
18561858
---------------------------------------------------------
18571859

Doc/library/stdtypes.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,17 @@ The constructors :func:`int`, :func:`float`, and
265265
pair: operator; % (percent)
266266
pair: operator; **
267267

268+
.. _stdtypes-mixed-arithmetic:
269+
268270
Python fully supports mixed arithmetic: when a binary arithmetic operator has
269-
operands of different numeric types, the operand with the "narrower" type is
270-
widened to that of the other, where integer is narrower than floating point.
271+
operands of different built-in numeric types, the operand with the "narrower"
272+
type is widened to that of the other:
273+
274+
* If both arguments are complex numbers, no conversion is performed;
275+
* if either argument is a complex or a floating-point number, the other is
276+
converted to a floating-point number;
277+
* otherwise, both must be integers and no conversion is necessary.
278+
271279
Arithmetic with complex and real operands is defined by the usual mathematical
272280
formula, for example::
273281

Doc/library/token.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ The token constants are:
5050

5151
.. data:: NAME
5252

53-
Token value that indicates an :ref:`identifier <identifiers>`.
54-
Note that keywords are also initially tokenized as ``NAME`` tokens.
53+
Token value that indicates an :ref:`identifier or keyword <identifiers>`.
5554

5655
.. data:: NUMBER
5756

Doc/reference/expressions.rst

Lines changed: 154 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ Expressions
99

1010
This chapter explains the meaning of the elements of expressions in Python.
1111

12-
**Syntax Notes:** In this and the following chapters, extended BNF notation will
13-
be used to describe syntax, not lexical analysis. When (one alternative of) a
14-
syntax rule has the form
12+
**Syntax Notes:** In this and the following chapters,
13+
:ref:`grammar notation <notation>` will be used to describe syntax,
14+
not lexical analysis.
15+
16+
When (one alternative of) a syntax rule has the form:
1517

1618
.. productionlist:: python-grammar
1719
name: othername
@@ -29,17 +31,13 @@ Arithmetic conversions
2931

3032
When a description of an arithmetic operator below uses the phrase "the numeric
3133
arguments are converted to a common real type", this means that the operator
32-
implementation for built-in types works as follows:
33-
34-
* If both arguments are complex numbers, no conversion is performed;
35-
36-
* if either argument is a complex or a floating-point number, the other is converted to a floating-point number;
37-
38-
* otherwise, both must be integers and no conversion is necessary.
34+
implementation for built-in numeric types works as described in the
35+
:ref:`Numeric Types <stdtypes-mixed-arithmetic>` section of the standard
36+
library documentation.
3937

40-
Some additional rules apply for certain operators (e.g., a string as a left
41-
argument to the '%' operator). Extensions must define their own conversion
42-
behavior.
38+
Some additional rules apply for certain operators and non-numeric operands
39+
(for example, a string as a left argument to the ``%`` operator).
40+
Extensions must define their own conversion behavior.
4341

4442

4543
.. _atoms:
@@ -49,15 +47,57 @@ Atoms
4947

5048
.. index:: atom
5149

52-
Atoms are the most basic elements of expressions. The simplest atoms are
53-
identifiers or literals. Forms enclosed in parentheses, brackets or braces are
54-
also categorized syntactically as atoms. The syntax for atoms is:
50+
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.
5554

56-
.. productionlist:: python-grammar
57-
atom: `identifier` | `literal` | `enclosure`
58-
enclosure: `parenth_form` | `list_display` | `dict_display` | `set_display`
59-
: | `generator_expression` | `yield_atom`
55+
Formally, the syntax for atoms is:
56+
57+
.. grammar-snippet::
58+
:group: python-grammar
59+
60+
atom:
61+
| 'True'
62+
| 'False'
63+
| 'None'
64+
| '...'
65+
| `identifier`
66+
| `literal`
67+
| `enclosure`
68+
enclosure:
69+
| `parenth_form`
70+
| `list_display`
71+
| `dict_display`
72+
| `set_display`
73+
| `generator_expression`
74+
| `yield_atom`
75+
76+
77+
.. _atom-singletons:
78+
79+
Built-in constants
80+
------------------
81+
82+
The keywords ``True``, ``False``, and ``None`` name
83+
:ref:`built-in constants <built-in-consts>`.
84+
The token ``...`` names the :py:data:`Ellipsis` constant.
6085

86+
Evaluation of these atoms yields the corresponding value.
87+
88+
.. note::
89+
90+
Several more built-in constants are available as global variables,
91+
but only the ones mentioned here are :ref:`keywords <keywords>`.
92+
In particular, these names cannot be reassigned or used as attributes:
93+
94+
.. code-block:: pycon
95+
96+
>>> False = 123
97+
File "<input>", line 1
98+
False = 123
99+
^^^^^
100+
SyntaxError: cannot assign to False
61101
62102
.. _atom-identifiers:
63103

@@ -131,51 +171,104 @@ Literals
131171

132172
.. index:: single: literal
133173

134-
Python supports string and bytes literals and various numeric literals:
174+
A :dfn:`literal` is a textual representation of a value.
175+
Python supports numeric, string and bytes literals.
176+
:ref:`Format strings <f-strings>` and :ref:`template strings <t-strings>`
177+
are treated as string literals.
178+
179+
Numeric literals consist of a single :token:`NUMBER <python-grammar:NUMBER>`
180+
token, which names an integer, floating-point number, or an imaginary number.
181+
See the :ref:`numbers` section in Lexical analysis documentation for details.
182+
183+
String and bytes literals may consist of several tokens.
184+
See section :ref:`string-concatenation` for details.
185+
186+
Note that negative and complex numbers, like ``-3`` or ``3+4.2j``,
187+
are syntactically not literals, but :ref:`unary <unary>` or
188+
:ref:`binary <binary>` arithmetic operations involving the ``-`` or ``+``
189+
operator.
190+
191+
Evaluation of a literal yields an object of the given type
192+
(:class:`int`, :class:`float`, :class:`complex`, :class:`str`,
193+
:class:`bytes`, or :class:`~string.templatelib.Template`) with the given value.
194+
The value may be approximated in the case of floating-point
195+
and imaginary literals.
196+
197+
The formal grammar for literals is:
135198

136199
.. grammar-snippet::
137200
:group: python-grammar
138201

139202
literal: `strings` | `NUMBER`
140203

141-
Evaluation of a literal yields an object of the given type (string, bytes,
142-
integer, floating-point number, complex number) with the given value. The value
143-
may be approximated in the case of floating-point and imaginary (complex)
144-
literals.
145-
See section :ref:`literals` for details.
146-
See section :ref:`string-concatenation` for details on ``strings``.
147-
148204

149205
.. index::
150206
triple: immutable; data; type
151207
pair: immutable; object
152208

209+
Literals and object identity
210+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211+
153212
All literals correspond to immutable data types, and hence the object's identity
154213
is less important than its value. Multiple evaluations of literals with the
155214
same value (either the same occurrence in the program text or a different
156215
occurrence) may obtain the same object or a different object with the same
157216
value.
158217

218+
.. admonition:: CPython implementation detail
219+
220+
For example, in CPython, *small* integers with the same value evaluate
221+
to the same object::
222+
223+
>>> x = 7
224+
>>> y = 7
225+
>>> x is y
226+
True
227+
228+
However, large integers evaluate to different objects::
229+
230+
>>> x = 123456789
231+
>>> y = 123456789
232+
>>> x is y
233+
False
234+
235+
This behavior may change in future versions of CPython.
236+
In particular, the boundary between "small" and "large" integers has
237+
already changed in the past.
238+
239+
CPython will emit a :py:exc:`SyntaxWarning` when you compare literals
240+
using ``is``::
241+
242+
>>> x = 7
243+
>>> x is 7
244+
<input>:1: SyntaxWarning: "is" with 'int' literal. Did you mean "=="?
245+
True
246+
247+
See :ref:`faq-identity-with-is` for more information.
248+
249+
:ref:`Template strings <t-strings>` are immutable but may reference mutable
250+
objects as :class:`~string.templatelib.Interpolation` values.
251+
For the purposes of this section, two t-strings have the "same value" if
252+
both their structure and the *identity* of the values match.
253+
254+
.. impl-detail::
255+
256+
Currently, each evaluation of a template string results in
257+
a different object.
258+
159259

160260
.. _string-concatenation:
161261

162262
String literal concatenation
163263
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164264

165-
Multiple adjacent string or bytes literals (delimited by whitespace), possibly
265+
Multiple adjacent string or bytes literals, possibly
166266
using different quoting conventions, are allowed, and their meaning is the same
167267
as their concatenation::
168268

169269
>>> "hello" 'world'
170270
"helloworld"
171271

172-
Formally:
173-
174-
.. grammar-snippet::
175-
:group: python-grammar
176-
177-
strings: ( `STRING` | `fstring`)+ | `tstring`+
178-
179272
This feature is defined at the syntactical level, so it only works with literals.
180273
To concatenate string expressions at run time, the '+' operator may be used::
181274

@@ -208,6 +301,13 @@ string literals::
208301
>>> t"Hello" t"{name}!"
209302
Template(strings=('Hello', '!'), interpolations=(...))
210303

304+
Formally:
305+
306+
.. grammar-snippet::
307+
:group: python-grammar
308+
309+
strings: (`STRING` | `fstring`)+ | `tstring`+
310+
211311

212312
.. _parenthesized:
213313

@@ -1390,8 +1490,9 @@ for the operands): ``-1**2`` results in ``-1``.
13901490

13911491
The power operator has the same semantics as the built-in :func:`pow` function,
13921492
when called with two arguments: it yields its left argument raised to the power
1393-
of its right argument. The numeric arguments are first converted to a common
1394-
type, and the result is of that type.
1493+
of its right argument.
1494+
Numeric arguments are first :ref:`converted to a common type <stdtypes-mixed-arithmetic>`,
1495+
and the result is of that type.
13951496

13961497
For int operands, the result has the same type as the operands unless the second
13971498
argument is negative; in that case, all arguments are converted to float and a
@@ -1477,9 +1578,10 @@ operators and one for additive operators:
14771578

14781579
The ``*`` (multiplication) operator yields the product of its arguments. The
14791580
arguments must either both be numbers, or one argument must be an integer and
1480-
the other must be a sequence. In the former case, the numbers are converted to a
1481-
common real type and then multiplied together. In the latter case, sequence
1482-
repetition is performed; a negative repetition factor yields an empty sequence.
1581+
the other must be a sequence. In the former case, the numbers are
1582+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>` and then
1583+
multiplied together. In the latter case, sequence repetition is performed;
1584+
a negative repetition factor yields an empty sequence.
14831585

14841586
This operation can be customized using the special :meth:`~object.__mul__` and
14851587
:meth:`~object.__rmul__` methods.
@@ -1507,7 +1609,8 @@ This operation can be customized using the special :meth:`~object.__matmul__` an
15071609
pair: operator; //
15081610

15091611
The ``/`` (division) and ``//`` (floor division) operators yield the quotient of
1510-
their arguments. The numeric arguments are first converted to a common type.
1612+
their arguments. The numeric arguments are first
1613+
:ref:`converted to a common type <stdtypes-mixed-arithmetic>`.
15111614
Division of integers yields a float, while floor division of integers results in an
15121615
integer; the result is that of mathematical division with the 'floor' function
15131616
applied to the result. Division by zero raises the :exc:`ZeroDivisionError`
@@ -1523,8 +1626,9 @@ The floor division operation can be customized using the special
15231626
pair: operator; % (percent)
15241627

15251628
The ``%`` (modulo) operator yields the remainder from the division of the first
1526-
argument by the second. The numeric arguments are first converted to a common
1527-
type. A zero right argument raises the :exc:`ZeroDivisionError` exception. The
1629+
argument by the second. The numeric arguments are first
1630+
:ref:`converted to a common type <stdtypes-mixed-arithmetic>`.
1631+
A zero right argument raises the :exc:`ZeroDivisionError` exception. The
15281632
arguments may be floating-point numbers, e.g., ``3.14%0.7`` equals ``0.34``
15291633
(since ``3.14`` equals ``4*0.7 + 0.34``.) The modulo operator always yields a
15301634
result with the same sign as its second operand (or zero); the absolute value of
@@ -1555,7 +1659,9 @@ floating-point number using the :func:`abs` function if appropriate.
15551659

15561660
The ``+`` (addition) operator yields the sum of its arguments. The arguments
15571661
must either both be numbers or both be sequences of the same type. In the
1558-
former case, the numbers are converted to a common real type and then added together.
1662+
former case, the numbers are
1663+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>` and then
1664+
added together.
15591665
In the latter case, the sequences are concatenated.
15601666

15611667
This operation can be customized using the special :meth:`~object.__add__` and
@@ -1570,8 +1676,9 @@ This operation can be customized using the special :meth:`~object.__add__` and
15701676
single: operator; - (minus)
15711677
single: - (minus); binary operator
15721678

1573-
The ``-`` (subtraction) operator yields the difference of its arguments. The
1574-
numeric arguments are first converted to a common real type.
1679+
The ``-`` (subtraction) operator yields the difference of its arguments.
1680+
The numeric arguments are first
1681+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>`.
15751682

15761683
This operation can be customized using the special :meth:`~object.__sub__` and
15771684
:meth:`~object.__rsub__` methods.

0 commit comments

Comments
 (0)