Double underscore before a name

The leading double underscore tells the Python interpreter to rewrite the name in order to avoid conflict in a subclass. Interpreter changes variable name with class extension and that feature known as the Mangling. 

Python3




class Myclass():
    def __init__(self):
        self.__variable = 10


Calling from Interpreter

testFile.py 

The Python interpreter modifies the variable name with ___. So Multiple times It uses as a Private member because another class can not access that variable directly. The main purpose for __ is to use variable /method in class only If you want to use it outside of the class you can make it public.

Python3




class Myclass():
    def __init__(self):
        self.__variable = 10
 
    def func(self)
    print(self.__variable)


Calling from Interpreter

 

Underscore (_) in Python

In this article, we are going to see Underscore (_) in Python.

Following are different places where “_” is used in Python:

  • Single Underscore:
    • Single Underscore in Interpreter
    • Single Underscore after a name
    • Single Underscore before a name
    • Single underscore in numeric literals
  • Double Underscore:
    • Double underscore before a name
    • Double underscore before and after a name

Similar Reads

Single Underscore

Example 1: Single Underscore In Interpreter:...

Double underscore before a name

...

Double underscore before and after a name

...

Contact Us