What is the double underscore in Python?
Abigail Rogers
Updated on April 27, 2026
In this regard, what is the use of double underscore in Python?
Double underscores are used for fully private variables. According to Python documentation: If your class is intended to be subclassed, and you have attributes that you do not want subclasses to use, consider naming them with double leading underscores and no trailing underscores.
One may also ask, why _ is used in Python? It's just a variable name, and it's conventional in python to use _ for throwaway variables. It just indicates that the loop variable isn't actually used. The python interpreter stores the last expression value to the special variable called _ . The underscore _ is also used for ignoring the specific values.
Secondly, what is the difference between _ and __ in Python?
The idea here is to give you the ability to override operators in your own classes. Sometimes it's just a hook python calls in specific situations. __init__(), for example, is called when the object is created so you can initialize it. __new__() is called to build the instance, and so on
What is Python __ Name __?
The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.