Single Underscore

In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way. Using leading underscores for functions in a module indicates it should not be imported from somewhere else.

From the PEP-8 style guide:

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

Double Underscore (Name Mangling)

From the Python docs:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

And a warning from the same page:

Name mangling is intended to give classes an easy way to define “private” instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private.

Example

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}
Answer from Andrew Keeton on Stack Overflow
Top answer
1 of 16
1685

Single Underscore

In a class, names with a leading underscore indicate to other programmers that the attribute or method is intended to be be used inside that class. However, privacy is not enforced in any way. Using leading underscores for functions in a module indicates it should not be imported from somewhere else.

From the PEP-8 style guide:

_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

Double Underscore (Name Mangling)

From the Python docs:

Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, so it can be used to define class-private instance and class variables, methods, variables stored in globals, and even variables stored in instances. private to this class on instances of other classes.

And a warning from the same page:

Name mangling is intended to give classes an easy way to define “private” instance variables and methods, without having to worry about instance variables defined by derived classes, or mucking with instance variables by code outside the class. Note that the mangling rules are designed mostly to avoid accidents; it still is possible for a determined soul to access or modify a variable that is considered private.

Example

>>> class MyClass():
...     def __init__(self):
...             self.__superprivate = "Hello"
...             self._semiprivate = ", world!"
...
>>> mc = MyClass()
>>> print mc.__superprivate
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: myClass instance has no attribute '__superprivate'
>>> print mc._semiprivate
, world!
>>> print mc.__dict__
{'_MyClass__superprivate': 'Hello', '_semiprivate': ', world!'}
2 of 16
589
  • _foo: Only a convention. A way for the programmer to indicate that the variable is private (whatever that means in Python).

  • __foo: This has real meaning. The interpreter replaces this name with _classname__foo as a way to ensure that the name will not overlap with a similar name in another class.

  • __foo__: Only a convention. A way for the Python system to use names that won't conflict with user names.

No other form of underscores have meaning in the Python world. Also, there's no difference between class, variable, global, etc in these conventions.

🌐
Real Python
realpython.com › python-double-underscore
Single and Double Underscores in Python Names – Real Python
August 18, 2025 - Note: It’s important to note that Python doesn’t name-mangle dunder names, even though they start with two leading underscores. That means you can do something like cart.__len__(), and it’ll work. However, calling these methods directly isn’t the recommended way to go. Finally, Python also defines some dunder names that refer not to special methods but to special attributes and variables.
Discussions

python - Finding a list of all double-underscore variables - Stack Overflow
Where can I find a list of all double-underscore variables that are commonly used in Python? In Python, variables starting and ending with double underscores are typically to store metadata or are ... More on stackoverflow.com
🌐 stackoverflow.com
where to find a list of all double underscore class methods
https://docs.python.org/3/reference/datamodel.html Also https://levelup.gitconnected.com/python-dunder-methods-ea98ceabad15 More on reddit.com
🌐 r/learnpython
3
3
November 28, 2020
What’s the Meaning of Single and Double Underscores In Python?
Thanks PEP 8 More on reddit.com
🌐 r/Python
58
702
January 25, 2022
I need help understanding the underscore in setters and getters
they sneakily set a second class attribute _house in the setter method for the house property. now, if student is an instance of the Student class, any time student.house is accessed, either retrieving that property or setting it, it goes through the second _house attribute. however i'm not sure this way adds anything to the code (perhaps it does). what i do know is that the official python docs do it differently, setting the attributes with the underscore in the __init__() method, just as you'd expect. https://docs.python.org/3/library/functions.html#property More on reddit.com
🌐 r/cs50
14
7
April 26, 2023
🌐
Reddit
reddit.com › r/python › what’s the meaning of single and double underscores in python?
r/Python on Reddit: What’s the Meaning of Single and Double Underscores In Python?
January 25, 2022 -

Have you ever been curious about the several meanings of underscores in Python? A little break-down?

- you can find detailed explanations and code snippets here

1️⃣ single leading underscore ("_var"): indicates that the variable is meant for internal use. This is not enforced by the interpreter and is rather a hint to the programmer.

2️⃣ single trailing underscore ("var_"): it's used to avoid conflicts with Python reserved keywords ("class_", "def_", etc.)

3️⃣ double leading underscores ("__var"): Triggers name mangling when used in a class context and is enforced by the Python interpreter. 
What this means is that it should be used to avoid your method is being overridden by a subclass or accessed accidentally.

4️⃣ double leading and trailing underscores ("__var__"): used for special methods defined in the Python language (ex. __init__, __len__, __call__, etc.). They should be avoided to use for your own attributes.

5️⃣ single underscore ("_"): Generally used as a temporary or unused variable. (If you don't use the running index of a for-loop, you can replace it with "_").

🌐
Medium
medium.com › python-explainers › single-and-double-underscores-in-python-explained-63a805ef34db
Single and Double Underscores in Python explained | by Leendert Coenen | Python explainers | Medium
February 28, 2023 - A single underscore is used to indicate a private variable or method (although this is not enforced), while a double underscore is used to implement name mangling for a variable or method.
🌐
GeeksforGeeks
geeksforgeeks.org › python › single-aad-double-underscores-in-python
Single and Double Underscores in Python - GeeksforGeeks
July 23, 2025 - Let us understand about double underscore in Python with the help of some examples: When double underscores are used as a prefix in a class attribute name, it triggers a mechanism known as name mangling.
🌐
Python Engineer
python-engineer.com › posts › double-single-leading-underscore
What is the meaning of single and double leading underscore in Python - Python Engineer
However, they can still be accessed ... s._Sample__bar # ok · Names with double leading and trailing underscores are reserved for special use in Python....
🌐
Scaler
scaler.com › home › topics › what is the meaning of double underscore in python
What is the Meaning of Double Underscore in Python? - Scaler Topics
April 12, 2024 - This name tangling is done to protect the variable from getting overridden when the ExampleClass is inherited i.e in the subclasses. Double underscore prefix is used in Python to avoid naming conflict.
Find elsewhere
🌐
dbader.org
dbader.org › blog › meaning-of-underscores-in-python
The Meaning of Underscores in Python – dbader.org
May 23, 2017 - 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 ...
🌐
GitHub
bic-berkeley.github.io › psych-214-fall-2016 › two_dunders.html
Two double underscore variables — Functional MRI methods
Experienced Python people often call these variables “dunder” variables, because they have double underscores on each side.
🌐
GeeksforGeeks
geeksforgeeks.org › python › underscore-_-python
Underscore (_) in Python - GeeksforGeeks
May 3, 2025 - 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.
🌐
DataCamp
datacamp.com › tutorial › role-underscore-python
Underscore in Python Tutorial : What is the purpose and meaning of _ & __ | DataCamp
October 26, 2018 - Double Pre Underscores tells the Python interpreter to rewrite the attribute name of subclasses to avoid naming conflicts. Name Mangling:- interpreter of the Python alters the variable name in a way that it is challenging to clash when the class is inherited.
🌐
Delft Stack
delftstack.com › home › howto › python › python double underscore
Double Underscore in Python | Delft Stack
October 10, 2023 - You can use the following code to explain the double leading and trailing underscore in Python. class A: def __init__(self): self.__foo__ = 10 print(A().__foo__) ... In the code above, the foo variable with a double underscore as prefix and postfix is ignored by the interpreter, and the name ...
🌐
Python Reference
python-reference.readthedocs.io › en › latest › dunders.html
Double Underscore Methods and Variables — Python Reference (The Right Way) 0.1 documentation
Double Underscore Methods and Variables · Edit on GitHub · Methods used for direct set, get and delete operations on attributes.
🌐
Medium
medium.com › @abhishekjainindore24 › single-underscore-and-double-underscore-in-python-oop-1a48b24acabb
Single underscore (_) and Double underscore (__) in Python OOP | by Abhishek Jain | Medium
February 4, 2024 - Single Underscore (_): Indicates weak "internal use" and serves as a convention. Accessible from outside the class without name modifications. Double Underscore (__): Introduces name mangling, changing the name to include the class name.
🌐
Medium
mike-vincent.medium.com › the-python-underscore-2626d5d4096f
The Python Underscore _. Read this guide to learn how to use the… | by Mike Vincent | Medium
March 10, 2025 - Double underscores (e.g., __variable) trigger name mangling, which changes the name of the variable to include the class name. This helps prevent name clashes in subclasses and is used for class-private attributes.
🌐
Medium
medium.com › @mo.sob7y111 › role-of-underscore-double-underscore-in-python-abbb1de78e8a
Role of Underscore(_)&double Underscore (__)in Python. | by Mohamed Sobhi | Medium
July 15, 2022 - The leading double underscore tells the Python interpreter to rewrite the name in order to avoid conflict in a subclass. 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 ...
🌐
LinkedIn
linkedin.com › pulse › understanding-use-single-underscore-double-python-srikanth-kumar
Understanding the Use of "_" (Single Underscore) and "__" (Double Underscores) in Python class
June 23, 2023 - Double underscores are used for name mangling, a technique that Python uses to avoid naming conflicts in subclasses. When a class attribute or method is prefixed with double underscores, its name gets modified to include the class name as a prefix.
🌐
Analytics Vidhya
analyticsvidhya.com › home › what is the role of underscore ( _ ) in python?
What Is The Role of Underscore ( _ ) in Python? - Analytics Vidhya
May 27, 2025 - In a class definition, when you prefix a name with double underscores (e.g., __variable), Python utilizes name mangling to render the name more unique, preventing inadvertent name conflicts in subclasses.
🌐
Better Programming
betterprogramming.pub › how-to-use-underscore-properly-in-python-37df5e05ba4c
How To Use the Underscore (_) Properly in Python | by Doron Chosnek | Better Programming
January 5, 2023 - The dir listing of the class instance shows that the only difference between _myprivate (single underscore) and __myreallyprivate (double underscore) is that __myreallyprivate is slightly obfuscated. I can overwrite both variables that the author originally intended to be private like this: instance._myprivate = 'overwritten!!' instance._MyClass__myreallyprivate = 'overwritten!!' You’re probably wondering what the purpose of the obfuscation of __myreallyprivate might be if it is easily detected with a simple dir or by reading Python documentation.