Skip to content

gh-148423: Add Examples for Private Mangling#148424

Closed
ghost wants to merge 4 commits intomainfrom
unknown repository
Closed

gh-148423: Add Examples for Private Mangling#148424
ghost wants to merge 4 commits intomainfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost 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
@ghost ghost changed the title Added Examples for Private Mangling Added Examples for Private Mangling (GH-148423) Apr 12, 2026
@ghost ghost changed the title Added Examples for Private Mangling (GH-148423) Add Examples for Private Mangling Apr 12, 2026
@ghost ghost 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
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
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
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 12, 2026

I'm pretty sure there was already an update on that elsewhere. I can't find the pR though. And I'm also not convinced by this change.

@picnixz picnixz closed this Apr 12, 2026
@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 12, 2026

The tutorial already contains an example and there is a link to https://docs.python.org/3/reference/expressions.html#index-5 already so I don't think we need more.

@ghost
Copy link
Copy Markdown
Author

ghost commented Apr 12, 2026

Thanks for the review

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