Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 4a06269

Browse files
committed
Issue #44 - updated "data hiding" section description.
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent e0e03f9 commit 4a06269

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

assets/img/wy_ban.jpg

294 KB
Loading

src/oop/intro.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,26 @@ Data Hiding
239239
===========
240240

241241
Many programming languages has *access modifiers* implemented. The Python has
242-
also, but actually no. It's implemented as a convention-level. All class's
243-
members starting with underscore are considered to be **protected** members
244-
and should not be accessible outside the class instance; on the other hand
245-
members starting with two underscores are considered to be **private** members
246-
and shouldn't be accessible outside the class instance as well.
242+
also.
243+
244+
.. image:: /../assets/img/wy_ban.jpg
245+
:align: center
246+
247+
"Private" instance variables that cannot be accessed except from inside an
248+
object don't exist in Python :cite:`docs-python:private-variables`. It's
249+
implemented as a convention-level.
250+
251+
A name prefixed with an underscore (e.g. ``_spam``) should be treated as
252+
a non-public part of the API (whether it is a function, a method or a data
253+
member). These should not be used outside the class itself and can be changed
254+
without notice.
255+
256+
Since there is a valid use case for class-private members (namely to avoid name
257+
clashes of names with names defined by subclasses), there is limited support
258+
for such a mechanism, called *name mangling*. Any identifier with at least two
259+
leading underscore (e.g. ``__spam``) is textually replaced with
260+
``_classname__spam``, where ``_classname`` is the current class name with
261+
leading underscore stripped.
247262

248263
.. code-block:: python
249264

src/refs.bib

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ @misc{docs-python:stdtypes
7373
}
7474

7575
@misc{docs-python:errors,
76-
title = "{Errors and Exceptions}",
76+
title = "{Python Documentation: Errors and Exceptions}",
7777
url = {https://docs.python.org/3.11/tutorial/errors.html},
78-
}
78+
}
79+
80+
@misc{docs-python:private-variables,
81+
title = "{Python Documentation: Private Variables}",
82+
url = {https://docs.python.org/3/tutorial/classes.html?highlight=private#private-variables},
83+
}

0 commit comments

Comments
 (0)