Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Name mangling
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Python === In [[Python (programming language)|Python]], mangling is used for class attributes that one does not want subclasses to use<ref>{{Cite web|url=https://www.python.org/dev/peps/pep-0008/#designing-for-inheritance|title = PEP 8 -- Style Guide for Python Code}}</ref> which are designated as such by giving them a name with two or more leading underscores and no more than one trailing underscore. For example, {{code|__thing}} will be mangled, as will {{code|___thing}} and {{code|__thing_}}, but {{code|__thing__}} and {{code|__thing___}} will not. Python's runtime does not restrict access to such attributes, the mangling only prevents name collisions if a derived class defines an attribute with the same name. On encountering name mangled attributes, Python transforms these names by prepending a single underscore and the name of the enclosing class, for example: <syntaxhighlight lang="pycon"> >>> class Test: ... def __mangled_name(self): ... pass ... def normal_name(self): ... pass >>> t = Test() >>> [attr for attr in dir(t) if "name" in attr] ['_Test__mangled_name', 'normal_name'] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)