gh-148423: Add Examples for Private Mangling#148424
gh-148423: Add Examples for Private Mangling#148424manoj-k-m wants to merge 4 commits intopython:mainfrom
Conversation
da-woods
left a comment
There was a problem hiding this comment.
I think this is trying to document a detail that simply should not be documented in a tutorial.
Doc/tutorial/classes.rst
Outdated
| __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) |
There was a problem hiding this comment.
Why? You're in the class scope. This is where private variables just work.
There was a problem hiding this comment.
Thanks for pointing that out , I have fixed the line
| .. code-block:: pycon | ||
|
|
||
| >>> john = Employee() | ||
| >>> john._Employee__dept |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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/