From 11b5045b0ce18fee8cc1023cc516aeb14ebf7bda Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Sat, 22 May 2021 04:06:46 -0700 Subject: [PATCH] bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513) (GH-26238) Clarify ``getattr`` and ``setattr`` requirements for accessing name-mangled attributes Co-Authored-By: Catalin Iacob (cherry picked from commit 2edaf6a4fb7e20324dde1423232f07211347f092) Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> --- Doc/library/functions.rst | 14 ++++++++++++++ Doc/reference/expressions.rst | 2 ++ 2 files changed, 16 insertions(+) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 3a222b1e679..ce0d5326178 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -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 ` 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 ` 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]) diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index 8fead33efb9..51fa750d95b 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -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