Skip to content

Commit 477788f

Browse files
committed
Fixups, and a new section on identity
1 parent d42489a commit 477788f

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
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/reference/expressions.rst

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ implementation for built-in numeric types works as described in
3636
library documentation.
3737

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

4242

@@ -70,6 +70,8 @@ Formally, the syntax for atoms is:
7070
| `list_display`
7171
| `dict_display`
7272
| `set_display`
73+
| `generator_expression`
74+
| `yield_atom`
7375
7476

7577
.. _atom-singletons:
@@ -86,8 +88,7 @@ Evaluation of these atoms yields the corresponding value.
8688
.. note::
8789

8890
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+
but only the ones mentioned here are :ref:`keywords <keywords>`.
9192
In particular, these names cannot be reassigned or used as attributes
9293

9394
.. code-block:: pycon
@@ -175,56 +176,93 @@ Python supports numeric, string and bytes literals.
175176
:ref:`Format strings <f-strings>` and :ref:`template strings <t-strings>`
176177
are treated as string literals.
177178

178-
Numeric literals consist of a single :token:`python-grammar:NUMBER` token,
179-
which names an integer, floating-point number, or an imaginary number.
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.
180181
See the :ref:`numbers` section in Lexical analysis documentation for details.
181182

182183
String and bytes literals may consist of several tokens.
183184
See section :ref:`string-concatenation` for details.
184185

185186
Note that negative and complex numbers, like ``-3`` or ``3+4.2j``,
186-
are syntactically not literals, but expressions :ref:`unary <unary>` or
187+
are syntactically not literals, but :ref:`unary <unary>` or
187188
:ref:`binary <binary>` arithmetic operations involving the ``-`` or ``+``
188189
operator.
189190

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

197+
The formal grammar for literals is:
198+
199+
.. grammar-snippet::
200+
:group: python-grammar
201+
202+
literal: `strings` | `NUMBER`
203+
204+
196205
.. index::
197206
triple: immutable; data; type
198207
pair: immutable; object
199208

209+
Literals and object identity
210+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211+
200212
All literals correspond to immutable data types, and hence the object's identity
201213
is less important than its value. Multiple evaluations of literals with the
202214
same value (either the same occurrence in the program text or a different
203215
occurrence) may obtain the same object or a different object with the same
204216
value.
205217

206-
.. impl-detail::
218+
.. admonition:: CPython implementation detail
207219

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.
220+
For example, in CPython, *small* integers with the same value evaluate
221+
to the same object::
213222

214-
The formal grammar for literals is:
223+
>>> x = 7
224+
>>> y = 7
225+
>>> x is y
226+
True
215227

216-
.. grammar-snippet::
217-
:group: python-grammar
228+
However, large integers evaluate to different objects::
218229

219-
literal: `strings` | `NUMBER`
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.
220258

221259

222260
.. _string-concatenation:
223261

224262
String literal concatenation
225263
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
226264

227-
Multiple adjacent string or bytes literals (delimited by whitespace), possibly
265+
Multiple adjacent string or bytes literals, possibly
228266
using different quoting conventions, are allowed, and their meaning is the same
229267
as their concatenation::
230268

@@ -268,7 +306,7 @@ Formally:
268306
.. grammar-snippet::
269307
:group: python-grammar
270308

271-
strings: ( `STRING` | `fstring`)+ | `tstring`+
309+
strings: (`STRING` | `fstring`)+ | `tstring`+
272310

273311

274312
.. _parenthesized:

0 commit comments

Comments
 (0)