Skip to content

Commit d42489a

Browse files
encukoublaisep
andcommitted
Rewording of some of the Atoms section
Co-authored-by: Blaise Pabon <blaise@gmail.com>
1 parent 5db331a commit d42489a

File tree

3 files changed

+126
-51
lines changed

3 files changed

+126
-51
lines changed

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: 115 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;
34+
implementation for built-in numeric types works as described in
35+
:ref:`Numeric Types <stdtypes-mixed-arithmetic>` section of the standard
36+
library documentation.
3737

38-
* otherwise, both must be integers and no conversion is necessary.
39-
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+
(e.g., a string as a left argument to the '%' operator).
40+
Extensions must define their own conversion behavior.
4341

4442

4543
.. _atoms:
@@ -49,15 +47,56 @@ 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+
74+
75+
.. _atom-singletons:
6076

77+
Built-in constants
78+
------------------
79+
80+
The keywords ``True``, ``False``, and ``None`` name
81+
:ref:`built-in constants <built-in-consts>`.
82+
The token ``...`` names the :py:data:`Ellipsis` constant.
83+
84+
Evaluation of these atoms yields the corresponding value.
85+
86+
.. note::
87+
88+
Several more built-in constants are available as global variables,
89+
but only the ones mentioned here are :ref:`keywords <keywords>` and have
90+
special support in the parser.
91+
In particular, these names cannot be reassigned or used as attributes
92+
93+
.. code-block:: pycon
94+
95+
>>> False = 123
96+
File "<input>", line 1
97+
False = 123
98+
^^^^^
99+
SyntaxError: cannot assign to False
61100
62101
.. _atom-identifiers:
63102

@@ -131,20 +170,28 @@ Literals
131170

132171
.. index:: single: literal
133172

134-
Python supports string and bytes literals and various numeric literals:
173+
A :dfn:`literal` is a textual representation of a value.
174+
Python supports numeric, string and bytes literals.
175+
:ref:`Format strings <f-strings>` and :ref:`template strings <t-strings>`
176+
are treated as string literals.
135177

136-
.. grammar-snippet::
137-
:group: python-grammar
178+
Numeric literals consist of a single :token:`python-grammar:NUMBER` token,
179+
which names an integer, floating-point number, or an imaginary number.
180+
See the :ref:`numbers` section in Lexical analysis documentation for details.
138181

139-
literal: `strings` | `NUMBER`
182+
String and bytes literals may consist of several tokens.
183+
See section :ref:`string-concatenation` for details.
140184

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``.
185+
Note that negative and complex numbers, like ``-3`` or ``3+4.2j``,
186+
are syntactically not literals, but expressions :ref:`unary <unary>` or
187+
:ref:`binary <binary>` arithmetic operations involving the ``-`` or ``+``
188+
operator.
147189

190+
Evaluation of a literal yields and object of the given type
191+
(:class:`int`, :class:`float`, :class:`complex`, :class:`str`,
192+
:class:`bytes`, or :class:`~string.templatelib.Template`) with the given value.
193+
The value may be approximated in the case of floating-point
194+
and imaginary literals.
148195

149196
.. index::
150197
triple: immutable; data; type
@@ -156,6 +203,21 @@ same value (either the same occurrence in the program text or a different
156203
occurrence) may obtain the same object or a different object with the same
157204
value.
158205

206+
.. impl-detail::
207+
208+
In CPython, each evaluation of a :ref:`template strings <t-strings>` results
209+
in a different object.
210+
Note that for two template strings to have the same value, the *identity*
211+
of the values of their :class:`~string.templatelib.Interpolation`\ s
212+
must match.
213+
214+
The formal grammar for literals is:
215+
216+
.. grammar-snippet::
217+
:group: python-grammar
218+
219+
literal: `strings` | `NUMBER`
220+
159221

160222
.. _string-concatenation:
161223

@@ -169,13 +231,6 @@ as their concatenation::
169231
>>> "hello" 'world'
170232
"helloworld"
171233

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

@@ -208,6 +263,13 @@ string literals::
208263
>>> t"Hello" t"{name}!"
209264
Template(strings=('Hello', '!'), interpolations=(...))
210265

266+
Formally:
267+
268+
.. grammar-snippet::
269+
:group: python-grammar
270+
271+
strings: ( `STRING` | `fstring`)+ | `tstring`+
272+
211273

212274
.. _parenthesized:
213275

@@ -1297,8 +1359,9 @@ for the operands): ``-1**2`` results in ``-1``.
12971359

12981360
The power operator has the same semantics as the built-in :func:`pow` function,
12991361
when called with two arguments: it yields its left argument raised to the power
1300-
of its right argument. The numeric arguments are first converted to a common
1301-
type, and the result is of that type.
1362+
of its right argument.
1363+
Numeric arguments are first :ref:`converted to a common type <stdtypes-mixed-arithmetic>`,
1364+
and the result is of that type.
13021365

13031366
For int operands, the result has the same type as the operands unless the second
13041367
argument is negative; in that case, all arguments are converted to float and a
@@ -1384,9 +1447,10 @@ operators and one for additive operators:
13841447

13851448
The ``*`` (multiplication) operator yields the product of its arguments. The
13861449
arguments must either both be numbers, or one argument must be an integer and
1387-
the other must be a sequence. In the former case, the numbers are converted to a
1388-
common real type and then multiplied together. In the latter case, sequence
1389-
repetition is performed; a negative repetition factor yields an empty sequence.
1450+
the other must be a sequence. In the former case, the numbers are
1451+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>` and then
1452+
multiplied together. In the latter case, sequence repetition is performed;
1453+
a negative repetition factor yields an empty sequence.
13901454

13911455
This operation can be customized using the special :meth:`~object.__mul__` and
13921456
:meth:`~object.__rmul__` methods.
@@ -1430,8 +1494,9 @@ The floor division operation can be customized using the special
14301494
pair: operator; % (percent)
14311495

14321496
The ``%`` (modulo) operator yields the remainder from the division of the first
1433-
argument by the second. The numeric arguments are first converted to a common
1434-
type. A zero right argument raises the :exc:`ZeroDivisionError` exception. The
1497+
argument by the second. The numeric arguments are first converted to a
1498+
:ref:`converted to a common type <stdtypes-mixed-arithmetic>`.
1499+
A zero right argument raises the :exc:`ZeroDivisionError` exception. The
14351500
arguments may be floating-point numbers, e.g., ``3.14%0.7`` equals ``0.34``
14361501
(since ``3.14`` equals ``4*0.7 + 0.34``.) The modulo operator always yields a
14371502
result with the same sign as its second operand (or zero); the absolute value of
@@ -1462,7 +1527,9 @@ floating-point number using the :func:`abs` function if appropriate.
14621527

14631528
The ``+`` (addition) operator yields the sum of its arguments. The arguments
14641529
must either both be numbers or both be sequences of the same type. In the
1465-
former case, the numbers are converted to a common real type and then added together.
1530+
former case, the numbers are
1531+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>` and then
1532+
added together.
14661533
In the latter case, the sequences are concatenated.
14671534

14681535
This operation can be customized using the special :meth:`~object.__add__` and
@@ -1477,8 +1544,9 @@ This operation can be customized using the special :meth:`~object.__add__` and
14771544
single: operator; - (minus)
14781545
single: - (minus); binary operator
14791546

1480-
The ``-`` (subtraction) operator yields the difference of its arguments. The
1481-
numeric arguments are first converted to a common real type.
1547+
The ``-`` (subtraction) operator yields the difference of its arguments.
1548+
The numeric arguments are first
1549+
:ref:`converted to a common real type <stdtypes-mixed-arithmetic>`.
14821550

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

0 commit comments

Comments
 (0)