N
The Daily Insight

What is the double underscore in Python?

Author

Abigail Rogers

Updated on April 27, 2026

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

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.

Related Question Answers

What is __ all __ in Python?

In other words, __all__ is a list of strings defining what symbols in a module will be exported when from <module> import * is used on the module. For example, the below-mentioned code saved as foo.py explicitly exports the symbols bar and baz: __all__ = ['bar', 'baz']

What is __ main __ in Python?

__name__ (A Special variable) in Python __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module's name.

What is def __ str __( self in Python?

Python __str__() This method is called when print() or str() function is invoked on an object. This method must return the String object. If we don't implement __str__() function for a class, then built-in object implementation is used that actually calls __repr__() function.

What is @property in Python?

@property is a built-in decorator for the property() function in Python. It is used to give "special" functionality to certain methods to make them act as getters, setters, or deleters when we define properties in a class.

What does _ mean in python for loop?

'_' is just a convention which indicates that you won't be needing the iterator variable inside the loop for any operations. Iterator variable needed: Sum up first 5 whole numbers. total = 0.

What is meant by in Python?

As other answers have said: The ** operator does exponentiation. a ** b is a raised to the b power. The same ** symbol is also used in function argument and calling notations, with a different meaning (passing and receiving arbitrary keyword arguments). The // operator does Python's version of integer division.

What are special methods in Python?

In Python, special methods are a set of predefined methods you can use to enrich your classes. They are easy to recognize because they start and end with double underscores, for example __init__ or __str__ .

What is a double in Python?

By default, python interprets any number that includes a decimal point as a double precision floating point number. The number a is called the mantissa of the number, while b is the exponent. In a double precision representation, the number a may be thought of as a sixteen-digit number between -1 and 1.

What is __ add __ in Python?

Learn how you should modify the __add__ method of a Python class to be able to add two instances of a custom object. We can define the __add__ method to return a Day instance with the total number of visits and contacts: class Day(object):

What is _ used for?

The symbol underscore ( _ ), also called underline, underdash, low line, or low dash, is a character that originally appeared on the typewriter and was primarily used to underline words.

What does double underscore mean?

A double underscore prefix causes the Python interpreter to rewrite the attribute name in order to avoid naming conflicts in subclasses. This is also called name mangling—the interpreter changes the name of the variable in a way that makes it harder to create collisions when the class is extended later.

What is self _ in Python?

Please understand. The underscore (_) is special in Python. While the underscore (_) is used for just snake-case variables and functions in most languages (Of course, not for all), but it has special meanings in Python. If you are python programmer, for _ in range(10) , __init__(self) like syntax may be familiar.

What is keyword in Python?

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7.

What is __ call __ Python?

The __call__ method enables Python programmers to write classes where the instances behave like functions. Both functions and the instances of such classes are called callables.

What is _ for?

Underscore. The symbol underscore ( _ ), also called underline, underdash, low line, or low dash, is a character that originally appeared on the typewriter and was primarily used to underline words.

What is __ Pycache __?

__pycache__ is a directory that contains bytecode cache files that are automatically generated by python, namely compiled python, or . pyc , files. You might be wondering why Python, an "interpreted" language, has any compiled files at all.