8000 gh-120452: improve documentation about private name mangling by picnixz · Pull Request #120451 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-120452: improve documentation about private name mangling #120451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Doc/faq/programming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,8 @@ Variable names with double leading underscores are "mangled" to provide a simple
but effective way to define class private variables. Any identifier of the form
``__spam`` (at least two leading underscores, at most one trailing underscore)
is textually replaced with ``_classname__spam``, where ``classname`` is the
current class name with any leading underscores stripped.
current class name with any leading underscores stripped
(see :ref:`here <private-name-mangling>` for details and special cases).

This doesn't guarantee privacy: an outside user can still deliberately access
the "_classname__spam" attribute, and private values are visible in the object's
Expand Down
32 changes: 21 additions & 11 deletions Doc/reference/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,27 @@ exception.
pair: name; mangling
pair: private; names

**Private name mangling:** When an identifier that textually occurs in a class
definition begins with two or more underscore characters and does not end in two
or more underscores, it is considered a :dfn:`private name` of that class.
Private names are transformed to a longer form before code is generated for
them. The transformation inserts the class name, with leading underscores
removed and a single underscore inserted, in front of the name. For example,
the identifier ``__spam`` occurring in a class named ``Ham`` will be transformed
to ``_Ham__spam``. This transformation is independent of the syntactical
context in which the identifier is used. If the transformed name is extremely
long (longer than 255 characters), implementation defined truncation may happen.
If the class name consists only of underscores, no transformation is done.
.. rubric:: Private name mangling

When an identifier that textually occurs in a class definition begins with two
or more underscore characters and does not end in two or more underscores, it
is considered a :dfn:`private name` of that class.

More precisely, private names are transformed to a longer form before code is
generated for them. The transformation rule is defined as follows:

- If the class name consists only of underscores, no transformation is done,
e.g., the identifier ``__spam`` occurring in a class named ``_`` or ``__``
is left as is.

- Otherwise, the transformation inserts the class name, with leading
underscores removed and a single underscore inserted, in front of
the identifier, e.g., the identifier ``__spam`` occurring in a class
named ``Ham``, ``_Ham`` or ``__Ham`` is transformed to ``_Ham__spam``.

The transformation is independent of the syntactical context in which the
identifier is used. Nonetheless, if the transformed name is extremely long
(longer than 255 characters), implementation-defined truncation may happen.


.. _atom-literals:
Expand Down
4 changes: 3 additions & 1 deletion Doc/tutorial/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ clashes of names with names defined by subclasses), there is limited support for
such a mechanism, called :dfn:`name mangling`. Any identifier of the form
``__spam`` (at least two leading underscores, at most one trailing underscore)
is textually replaced with ``_classname__spam``, where ``classname`` is the
current class name with leading underscore(s) stripped. This mangling is done
current class name with leading underscore(s) stripped. [#]_ This mangling is done
without regard to the syntactic position of the identifier, as long as it
occurs within the definition of a class.

Expand Down Expand Up @@ -930,3 +930,5 @@ Examples::
namespace; the name :attr:`~object.__dict__` is an attribute but not a global name.
Obviously, using this violates the abstraction of namespace implementation, and
should be restricted to things like post-mortem debuggers.

.. [#] See :ref:`here <private-name-mangling>` for details and special cases.
Loading
0