In name mangling process any identifier with two leading underscore and one trailing underscore is textually replaced with
_classname__identifier where classname is the name of the current class. It means that any identifier of the form __geek (at least two leading underscores or at most one trailing underscore) is replaced with _classname__geek, where classname is the current class name with leading underscore(s) stripped.
Traceback (most recent call last): File "/home/be691046ea08cd2db075d27186ea0493.py", line 14, in print(s1.__name) AttributeError: 'Student' object has no attribute '__name'
In the above example, the class variable
__name is not accessible outside the class. It can be accessed only within the class. Any modification of the class variable can be done only inside the class.
Name mangling process
With the help of
dir() method, we can see the name mangling process that is done to the class variable. The name mangling process was done by the Interpreter. The
dir() method is used by passing the class object and it will return all valid attributes that belong to that object.
[‘_Student__name’, ‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’]
The above output shows
dir() method returning all valid attributes with the name mangling process that is done to the class variable __name. The name changed from __name to _Student__name.
Accessing Name Mangled variables
The name mangling process helps to access the class variables from outside the class. The class variables can be accessed by adding _classname to it. The name mangling is closest to private not exactly private.
The above class variable is accessed by adding the _classname to it. The class variable is accessed from outside the class with the name _Student__name.
Name mangling with method overriding
Due to name mangling, there is limited support for a valid use-case for class-private members basically to avoid name clashes of names with names defined by subclasses. Any identifier of the form __geek (at least two leading underscores or at most one trailing underscore) is replaced with _classname__geek, where classname is the current class name with leading underscore(s) stripped. As long as it occurs within the definition of the class, this mangling is done. This is helpful for letting subclasses override methods without breaking intraclass method calls.
Let’s look at this example and try to find out how this underscore works:
In parent class In Child class
- Python - Read blob object in python using wand library
- Python | Index of Non-Zero elements in Python list
- Python | PRAW - Python Reddit API Wrapper
- Important differences between Python 2.x and Python 3.x with examples
- Python | Merge Python key values to list
- MySQL-Connector-Python module in Python
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- twitter-text-python (ttp) module - Python
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- SHA in Python
- SQL using Python | Set 1
- try-except vs If in Python
- Python if else
- pow() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.