[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 76bdeebef6)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-08-07 17:41:26 +02:00 committed by GitHub
parent 04d6de66c7
commit 59be613137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 6 deletions

View File

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