Skip to content

Commit 59be613

Browse files
miss-islingtonpicnixzAA-Turner
authored
[3.13] gh-122511: Improve documentation for object identity of mutable/immutable types (GH-122512) (#122778)
gh-122511: Improve documentation for object identity of mutable/immutable types (GH-122512) (cherry picked from commit 76bdeeb) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 04d6de6 commit 59be613

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Doc/reference/datamodel.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ that mutable object is changed.
106106
Types affect almost all aspects of object behavior. Even the importance of
107107
object identity is affected in some sense: for immutable types, operations that
108108
compute new values may actually return a reference to any existing object with
109-
the same type and value, while for mutable objects this is not allowed. E.g.,
110-
after ``a = 1; b = 1``, ``a`` and ``b`` may or may not refer to the same object
111-
with the value one, depending on the implementation, but after ``c = []; d =
112-
[]``, ``c`` and ``d`` are guaranteed to refer to two different, unique, newly
113-
created empty lists. (Note that ``c = d = []`` assigns the same object to both
114-
``c`` and ``d``.)
109+
the same type and value, while for mutable objects this is not allowed.
110+
For example, after ``a = 1; b = 1``, *a* and *b* may or may not refer to
111+
the same object with the value one, depending on the implementation.
112+
This is because :class:`int` is an immutable type, so the reference to ``1``
113+
can be reused. This behaviour depends on the implementation used, so should
114+
not be relied upon, but is something to be aware of when making use of object
115+
identity tests.
116+
However, after ``c = []; d = []``, *c* and *d* are guaranteed to refer to two
117+
different, unique, newly created empty lists. (Note that ``e = f = []`` assigns
118+
the *same* object to both *e* and *f*.)
115119

116120

117121
.. _types:

0 commit comments

Comments
 (0)