]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 22 May 2021 11:06:46 +0000 (04:06 -0700)
committerGitHub <noreply@github.com>
Sat, 22 May 2021 11:06:46 +0000 (12:06 +0100)
Clarify ``getattr`` and ``setattr`` requirements for accessing name-mangled attributes

Co-Authored-By: Catalin Iacob <iacobcatalin@gmail.com>
(cherry picked from commit 2edaf6a4fb7e20324dde1423232f07211347f092)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Doc/library/functions.rst
Doc/reference/expressions.rst

index 3a222b1e67975b64706f4b0699bb81a28f1d43e8..ce0d532617806eced6b21d79499ac0f661ea8a73 100644 (file)
@@ -676,6 +676,13 @@ are always available.  They are listed here in alphabetical order.
    ``x.foobar``.  If the named attribute does not exist, *default* is returned if
    provided, otherwise :exc:`AttributeError` is raised.
 
+   .. note::
+
+      Since :ref:`private name mangling <private-name-mangling>` happens at
+      compilation time, one must manually mangle a private attribute's
+      (attributes with two leading underscores) name in order to retrieve it with
+      :func:`getattr`.
+
 
 .. function:: globals()
 
@@ -1495,6 +1502,13 @@ are always available.  They are listed here in alphabetical order.
    object allows it.  For example, ``setattr(x, 'foobar', 123)`` is equivalent to
    ``x.foobar = 123``.
 
+   .. note::
+
+      Since :ref:`private name mangling <private-name-mangling>` happens at
+      compilation time, one must manually mangle a private attribute's
+      (attributes with two leading underscores) name in order to set it with
+      :func:`setattr`.
+
 
 .. class:: slice(stop)
            slice(start, stop[, step])
index 8fead33efb911dd7fc985498f8f1b98101494ce3..51fa750d95b1b9f3b827fdbad8331e3a4f8a0188 100644 (file)
@@ -77,6 +77,8 @@ When the name is bound to an object, evaluation of the atom yields that object.
 When a name is not bound, an attempt to evaluate it raises a :exc:`NameError`
 exception.
 
+.. _private-name-mangling:
+
 .. index::
    pair: name; mangling
    pair: private; names