8000 address rewiew comments · python/cpython@7f55716 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f55716

Browse files
committed
address rewiew comments
1 parent 8e138a4 commit 7f55716

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

Doc/faq/programming.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,14 +1739,17 @@ Variable names with double leading underscores are "mangled" to provide a simple
17391739
but effective way to define class private variables. Any identifier of the form
17401740
``__spam`` (at least two leading underscores, at most one trailing underscore)
17411741
is textually replaced with ``_classname__spam``, where ``classname`` is the
1742-
current class name with any leading underscores stripped
1743-
(see :ref:`here <private-name-mangling>` for details and special cases).
1742+
current class name with any leading underscores stripped.
17441743

17451744
This doesn't guarantee privacy: an outside user can still deliberately access
17461745
the "_classname__spam" attribute, and private values are visible in the object's
17471746
``__dict__``. Many Python programmers never bother to use private variable
17481747
names at all.
17491748

1749+
.. seealso::
1750+
1751+
The :ref:`private name mangling specifications <private-name-mangling>`
1752+
for details and special cases.
17501753

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

Doc/reference/expressions.rst

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,71 @@ or more underscore characters and does not end in two or more underscores, it
9090
is considered a :dfn:`private name` of that class.
9191

9292
More precisely, private names are transformed to a longer form before code is
93-
generated for them. The transformation rule is defined as follows:
93+
generated for them. If the transformed name is longer than 255 characters,
94+
implementation-defined truncation may happen.
9495

95-
- If the class name consists only of underscores, no transformation is done,
96-
e.g., the identifier ``__spam`` occurring in a class named ``_`` or ``__``
97-
is left as is.
96+
The transformation is independent of the syntactical context in which the
97+
identifier is used and the transformation rule is defined as follows:
98+
99+
- If the class name consists only of underscores, the transformation is the
100+
identity, e.g., the identifier ``__spam`` occurring in a class named ``_``
101+
or ``__`` is left as is.
98102

99103
- Otherwise, the transformation inserts the class name, with leading
100104
underscores removed and a single underscore inserted, in front of
101105
the identifier, e.g., the identifier ``__spam`` occurring in a class
102-
named ``Ham``, ``_Ham`` or ``__Ham`` is transformed to ``_Ham__spam``.
106+
named ``Foo``, ``_Foo`` or ``__Foo`` is transformed to ``_Foo__spam``.
103107

104-
The transformation is independent of the syntactical context in which the
105-
identifier is used. Nonetheless, if the transformed name is extremely long
106-
(longer than 255 characters), implementation-defined truncation may happen.
108+
For identifiers declared using :keyword:`import` statements, this rule is
109+
slightly different. Indeed, importing a module with a private name directly
110+
in a class body raises a :exc:`ModuleNotFoundError` (unless the class name
111+
only consists of underscores), as illustrated by the following example:
112+
113+
.. code-block:: python
114+
115+
class Foo:
116+
import __spam # raises ModuleNotFoundError at runtime
117+
118+
This restriction can be lifted by using the :func:`__import__` function,
119+
in which case the transformation rule is applied normally:
120+
121+
.. code-block:: python
122+
123+
class Foo:
124+
__spam = __import__("__spam")
125+
126+
Foo._Foo__spam.do()
127+
128+
.. note::
129+
130+
This restriction does not apply to modules imported as submodules of
131+
private packages, e.g.:
132+
133+
.. code-block:: python
134+
135+
class Foo:
136+
import __spam.util
137+
138+
Foo._Foo__spam.util.do()
139+
140+
The corresponding private member is accessed by its defining class using
141+
its non-transformed name. On the other hand, the transformed name must be
142+
used to access the private member *externally* (e.g., in a function or in
143+
a subclass):
144+
145+
.. code-block:: python
146+
147+
class A:
148+
def __one(self):
149+
return 1
150+
def two(self):
151+
return 2 * self.__one()
152+
153+
class B(A):
154+
def three(self):
155+
return 3 * self._A__one()
156+
157+
four = A()._A__one()
107158
108159
109160
.. _atom-literals:

Doc/tutorial/classes.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,15 @@ clashes of names with names defined by subclasses), there is limited support for
684684
such a mechanism, called :dfn:`name mangling`. Any identifier of the form
685685
``__spam`` (at least two leading underscores, at most one trailing underscore)
686686
is textually replaced with ``_classname__spam``, where ``classname`` is the
687-
current class name with leading underscore(s) stripped. [#]_ This mangling is done
687+
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

@@ -930,5 +935,3 @@ Examples::
930935
namespace; the name :attr:`~object.__dict__` is an attribute but not a global name.
931936
Obviously, using this violates the abstraction of namespace implementation, and
932937
should be restricted to things like post-mortem debuggers.
933-
934-
.. [#] See :ref:`here <private-name-mangling>` for details and special cases.

0 commit comments

Comments
 (0)
0