What is the Meaning of Double Underscore in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Python interpreter uses the double underscore to avoid naming conflict in Python subclasses. It uses the double underscore to rewrite the attribute name in Python classes.

The rewriting of the attribute name in order to avoid collision is called name mangling.

Double Underscore as Prefix in Python

The syntax for double underscore as a prefix in Python is as:

The double underscore tells the Python interpreter to rewrite the attribute names in the Python class to avoid any naming collision when the Python class is inherited.

Let's take an example to understand the use of double underscore as a prefix in Python.

Output:

DOUBLE UNDERSCORE AS PREFIX

Explanation:

In the above code, the DemoClass uses the variables var1,var2, and var3 to store values. These values can be modified depending on the condition passed in the program.

The dir(obj1) commands gives all the list of valid attributes in the class whose object is passed in the dir() function.

As in the above output, we can see that var1 and _var2 variable names are identical in the output but the variable __var3 is changed to _DemoClass__var3. This name tangling is done to protect the variable from getting overridden when the DemoClass is inherited i.e in the subclasses.

Leading and Trailing Double Underscore

The syntax for leading and trailing double underscores in Python is as:

The process of rewriting the attribute name doesn't takes place i.e name mangling does not occur when leading and trailing underscore are applied around a variable.

Let's take an example to understand the use of leading and trailing underscores.

Output :

Explanation:

In the above code, the Python interpreter ignores the trailing and leading underscore before and after variable var1. So, name mangling does not occur in the case of leading and trailing underscores. The Demo().var1 returns the actual value stored in the var1 variable i.e the output is 12.

Some unique names in Python like init and call with leading and trailing underscores are reserved in Python for a particular use.

Examples of Double Underscore in Python

Let's take an example of a Double underscore in Python to understand its use.

Output :

OUTPUT OF DOUBLE UNDERSCORE

Explanation:

In the above code, the ExampleClass uses the variables var1, baz, and foo to store values. These values can be modified depending on the condition passed in the program.

The dir(exampleObj) commands gives all the list of valid attributes in the class whose object is passed in the dir() function.

As in the above output, we can see that var1 and _baz variable names are same in the output but the variable __foo is changed to _ExampleClass__foo. This name tangling is done to protect the variable from getting overridden when the ExampleClass is inherited i.e in the subclasses.

Conclusion

  • Double underscore prefix is used in Python to avoid naming conflict.
  • Rewriting attribute names to avoid naming collision is called name mangling.
  • Leading and trailing double underscore does not rewrite the attribute name.
  • Name mangling does not occur in the case of leading and trailing double underscore.