-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-148423: Add Examples for Private Mangling #148424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:: | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.