Skip to content
Closed
Changes from all 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
17 changes: 16 additions & 1 deletion Doc/tutorial/classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,22 @@ such a mechanism, called :dfn:`name mangling`. Any identifier of the form
is textually replaced with ``_classname__spam``, where ``classname`` is the
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.
occurs within the definition of a class. For example:

.. code-block:: python

class Employee:
__dept = 'computer lab'
def __detail(self):
print('Employee is from',self.__dept)

.. code-block:: pycon

>>> john = Employee()
>>> john._Employee__dept
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of private variables in that you don't access them outside of the class scope. Yes the name is mangled and so you can. But you shouldn't, so the tutorial shouldn't show people how.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback . But knowing how the variables are accessed can help them prevent name collisions in inheritance right ? . I could adjust the documentation accordingly.

'computer lab'
>>> john._Employee__detail()
Employee is from computer lab

.. seealso::

Expand Down
Loading