Skip to content

gh-148423: Add Examples for Private Mangling#148424

Open
manoj-k-m wants to merge 4 commits intopython:mainfrom
manoj-k-m:add-private-mangling-doc
Open

gh-148423: Add Examples for Private Mangling#148424
manoj-k-m wants to merge 4 commits intopython:mainfrom
manoj-k-m:add-private-mangling-doc

Conversation

@manoj-k-m
Copy link
Copy Markdown
Contributor

@manoj-k-m manoj-k-m commented Apr 12, 2026

Fixes gh-148423

Added Private name mangling example for both methods and variables in Doc/tutorial/classes.rst


📚 Documentation preview 📚: https://cpython-previews--148424.org.readthedocs.build/

@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Apr 12, 2026
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Apr 12, 2026
@manoj-k-m manoj-k-m changed the title Added Examples for Private Mangling Added Examples for Private Mangling (GH-148423) Apr 12, 2026
@manoj-k-m manoj-k-m changed the title Added Examples for Private Mangling (GH-148423) Add Examples for Private Mangling Apr 12, 2026
@manoj-k-m manoj-k-m changed the title Add Examples for Private Mangling Add Examples for Private Mangling (GH-148423) Apr 12, 2026
Copy link
Copy Markdown
Contributor

@da-woods da-woods left a comment

Choose a reason for hiding this comment

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

I think this is trying to document a detail that simply should not be documented in a tutorial.

__dept = 'computer lab'
def __detail(self):
# __dept is a private variable,so we access it with _Employee__dept
print('Employee is from',self._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.

Why? You're in the class scope. This is where private variables just work.

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 pointing that out , I have fixed the line

.. 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.

@StanFromIreland StanFromIreland changed the title Add Examples for Private Mangling (GH-148423) gh-148423: Add Examples for Private Mangling Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Add Examples for private name mangling

3 participants