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 <iacobcatalin@gmail.com>
(cherry picked from commit 2edaf6a4fb)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2021-05-22 04:06:46 -07:00 committed by GitHub
parent a373c80811
commit 11b5045b0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View 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 ``x.foobar``. If the named attribute does not exist, *default* is returned if
provided, otherwise :exc:`AttributeError` is raised. 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() .. 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 object allows it. For example, ``setattr(x, 'foobar', 123)`` is equivalent to
``x.foobar = 123``. ``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) .. class:: slice(stop)
slice(start, stop[, step]) slice(start, stop[, step])

View 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` When a name is not bound, an attempt to evaluate it raises a :exc:`NameError`
exception. exception.
.. _private-name-mangling:
.. index:: .. index::
pair: name; mangling pair: name; mangling
pair: private; names pair: private; names