@@ -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
0 commit comments