Skip to content

Commit 4af9c05

Browse files
miss-islingtonpicnixzJelleZijlstra
authored
[3.13] gh-120452: improve documentation about private name mangling (GH-120451) (#121715)
gh-120452: improve documentation about private name mangling (GH-120451) (cherry picked from commit f4d6e45) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent b9cfb81 commit 4af9c05

File tree

3 files changed

+69
-15
lines changed

3 files changed

+69
-15
lines changed

Doc/faq/programming.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,11 +1741,31 @@ but effective way to define class private variables. Any identifier of the form
17411741
is textually replaced with ``_classname__spam``, where ``classname`` is the
17421742
current class name with any leading underscores stripped.
17431743

1744-
This doesn't guarantee privacy: an outside user can still deliberately access
1745-
the "_classname__spam" attribute, and private values are visible in the object's
1746-
``__dict__``. Many Python programmers never bother to use private variable
1747-
names at all.
1744+
The identifier can be used unchanged within the class, but to access it outside
1745+
the class, the mangled name must be used:
17481746

1747+
.. code-block:: python
1748+
1749+
class A:
1750+
def __one(self):
1751+
return 1
1752+
def two(self):
1753+
return 2 * self.__one()
1754+
1755+
class B(A):
1756+
def three(self):
1757+
return 3 * self._A__one()
1758+
1759+
four = 4 * A()._A__one()
1760+
1761+
In particular, this does not guarantee privacy since an outside user can still
1762+
deliberately access the private attribute; many Python programmers never bother
1763+
to use private variable names at all.
1764+
1765+
.. seealso::
1766+
1767+
The :ref:`private name mangling specifications <private-name-mangling>`
1768+
for details and special cases.
17491769

17501770
My class defines __del__ but it is not called when I delete the object.
17511771
-----------------------------------------------------------------------

Doc/reference/expressions.rst

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,47 @@ exception.
8383
pair: name; mangling
8484
pair: private; names
8585

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

89+
When an identifier that textually occurs in a class definition begins with two
90+
or more underscore characters and does not end in two or more underscores, it
91+
is considered a :dfn:`private name` of that class.
92+
93+
.. seealso::
94+
95+
The :ref:`class specifications <class>`.
96+
97+
More precisely, private names are transformed to a longer form before code is
98+
generated for them. If the transformed name is longer than 255 characters,
99+
implementation-defined truncation may happen.
100+
101+
The transformation is independent of the syntactical context in which the
102+
identifier is used but only the following private identifiers are mangled:
103+
104+
- Any name used as the name of a variable that is assigned or read or any
105+
name of an attribute being accessed.
106+
107+
The ``__name__`` attribute of nested functions, classes, and type aliases
108+
is however not mangled.
109+
110+
- The name of imported modules, e.g., ``__spam`` in ``import __spam``.
111+
If the module is part of a package (i.e., its name contains a dot),
112+
the name is *not* mangled, e.g., the ``__foo`` in ``import __foo.bar``
113+
is not mangled.
114+
115+
- The name of an imported member, e.g., ``__f`` in ``from spam import __f``.
116+
117+
The transformation rule is defined as follows:
118+
119+
- The class name, with leading underscores removed and a single leading
120+
underscore inserted, is inserted in front of the identifier, e.g., the
121+
identifier ``__spam`` occurring in a class named ``Foo``, ``_Foo`` or
122+
``__Foo`` is transformed to ``_Foo__spam``.
123+
124+
- If the class name consists only of underscores, the transformation is the
125+
identity, e.g., the identifier ``__spam`` occurring in a class named ``_``
126+
or ``__`` is left as is.
98127

99128
.. _atom-literals:
100129

Doc/tutorial/classes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,11 @@ current class name with leading underscore(s) stripped. This mangling is done
688688
without regard to the syntactic position of the identifier, as long as it
689689
occurs within the definition of a class.
690690

691+
.. seealso::
692+
693+
The :ref:`private name mangling specifications <private-name-mangling>`
694+
for details and special cases.
695+
691696
Name mangling is helpful for letting subclasses override methods without
692697
breaking intraclass method calls. For example::
693698

0 commit comments

Comments
 (0)