Skip to content

Commit 2edaf6a

Browse files
bpo-8264: Document hasattr and getattr behavior for private attributes (pythonGH-23513)
Clarify ``getattr`` and ``setattr`` requirements for accessing name-mangled attributes Co-Authored-By: Catalin Iacob <[email protected]>
1 parent bb739ec commit 2edaf6a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Doc/library/functions.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,13 @@ are always available. They are listed here in alphabetical order.
693693
``x.foobar``. If the named attribute does not exist, *default* is returned if
694694
provided, otherwise :exc:`AttributeError` is raised.
695695

696+
.. note::
697+
698+
Since :ref:`private name mangling <private-name-mangling>` happens at
699+
compilation time, one must manually mangle a private attribute's
700+
(attributes with two leading underscores) name in order to retrieve it with
701+
:func:`getattr`.
702+
696703

697704
.. function:: globals()
698705

@@ -1512,6 +1519,13 @@ are always available. They are listed here in alphabetical order.
15121519
object allows it. For example, ``setattr(x, 'foobar', 123)`` is equivalent to
15131520
``x.foobar = 123``.
15141521

1522+
.. note::
1523+
1524+
Since :ref:`private name mangling <private-name-mangling>` happens at
1525+
compilation time, one must manually mangle a private attribute's
1526+
(attributes with two leading underscores) name in order to set it with
1527+
:func:`setattr`.
1528+
15151529

15161530
.. class:: slice(stop)
15171531
slice(start, stop[, step])

Doc/reference/expressions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ When the name is bound to an object, evaluation of the atom yields that object.
7777
When a name is not bound, an attempt to evaluate it raises a :exc:`NameError`
7878
exception.
7979

80+
.. _private-name-mangling:
81+
8082
.. index::
8183
pair: name; mangling
8284
pair: private; names

0 commit comments

Comments
 (0)