Table of Content
- What is Name Mangling in Python?
- What is the Use of Name Mangling in Python
- How to Use Name Mangling With Class Attributes in Python
- How to Access Name Mangled Attributes in Python
- How to Use Name Mangling with Method Names in Python
- Special Behavior in Name Mangling
- Name mangling with method overriding
- Wrap Off
Python does not have an explicit access modifier, which means you cannot make a class attribute or method to be public/private.
However, class attributes or methods can be made private using a process called name mangling in Python.
In this article, you will learn a very important concept in Python called Name Mangling and how it works.
What is Name Mangling in Python?
Name mangling in Python is the process of an identifier with two leading underscores getting replaced with the class name followed by the identifier, e.g.
Where class name is the name of the current class.
It means that any identifier of the form __var_name — at least two leading underscores or at most one trailing underscore — is replaced with _classname__var_name.
What is the Use of Name Mangling in Python
Name mangling in Python is used to restrict private class attributes or methods from being accessed from outside the class.
Most high-level programming languages have a way of making the attributes and methods of the class to be private or protected.
Python does not have an access modifier, hence, you cannot restrict the class attributes and methods from getting access from outside the class.
To partially implement this, Python uses the concept of Name Mangling.
How to Use Name Mangling With Class Attributes in Python
Let's take a simple class example to show how name mangling works in Python:
Now, what do you think the values of
__baz will be on instances of this
Let's take a look:
Wait, why did we get that
AttributeError when we tried to access the value of
This is python name mangling at work!
With Name mangling, it turns out this instance of the car class does not even have a
Let's check the
dir of the
From the output below, you will notice the presence of the
foo attribute in the list.
__baz attribute is missing.
How to Access Name Mangled Attributes in Python
You can access name mangled attributes in python in two ways:
1. Accessing attributes via attribute name
One way to access an attribute with name mangling is through the name of the attribute as listed in the
It follows the format:
Example of Accessing attributes via attribute name
To access the value of
__baz, you can simply just call
_Car__baz on the instance.
2. Accessing attributes via instance methods
Another way you can access the value of an attribute with name mangling is by creating a getter method and returning the value of the mangled attribute.
Example of Accessing attributes via instance methods
How to Use Name Mangling with Method Names in Python
Name mangling also applies to method names.
It affects all names that start with two underscore characters in a class context:
If you call the __drive method in the class above on the instance, you will get
You can access it by calling the method through a non-mangled method or using the class and method name.
Special Behavior in Name Mangling
Here's another unexpected example of name mangling in action.
It shows that name mangling isn't tied to class attributes specifically.
It applies to any name starting with two underscore characters used in a class context.
In the example above, we declared a global variable called
Then we accessed the variable inside the context of a class named
Because of name mangling, we will be able to reference the
_Car__mileage global variable as just
__mileage inside the
get_mileage() method on the class.
The Python interpreter automatically expands the name
_Car__mileage because it begins with two underscore characters.
Name mangling with method overriding
Using inheritance in object-oriented programming, you can override the base class method with a derived class method.
Supposing you want to write a method with the same name in the parent as well as in the child's class.
There is limited support in Python for a valid use case for class-private members to avoid name clashes with names defined by subclasses.
To avoid clashing when the parent class is inherited, you can add
__ (double underscore) in front of the method name.
Both methods will be renamed through name mangling using the format below.
Name Mangling in the base class:
Name Mangling in the derived class:
Let's look at this example and find out how this works.
Example of Name mangling with method overriding
# Base class class Vehicle: def __init__(self): self.__info() # name mangling with the info method # default method for name mangling in the parent class def info(self): print('Print from Vehicle class') # private copy of original info() method for private class members __info = info # Name mangling with method overriding process # Derived class class Car(Vehicle): # provides a new signature for info() but # does not break __init__() def info(self): print('Print from Car class') # Create an object of the Car class car = Car() car.info()
We have learned about the introduction of the Name mangling process in this article.
We also learned how we can use the dir() method for name mangling in Python.
Then, we accessed the name mangled variables outside the class in which they are present.
Lastly, we also learned about how we can use name mangling process with method overriding in given subclasses in the program.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.
Please share this post and for more insightful posts on business, technology, engineering, history, and marketing, subscribe to our newsletter.