Python
underscore
object naming
programming conventions
code readability

What is the meaning of single and double underscore before an object name?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Python, underscores are often used as a naming convention to convey the intended use or scope of variables, functions, or methods. Understanding the difference between a single underscore (_) and a double underscore (__) before an object name can be crucial for writing effective and idiomatic Python code. These underscores serve various purposes related to encapsulation, avoidance of name clashes, and adherence to coding conventions.

Single Underscore (_)

Meaning and Use Cases

The single underscore is primarily a convention used within the Python community to indicate that a variable or method is intended for internal use. This is a weaker form of protection, relying more on convention rather than enforcing access restrictions through language constructs.

  • Internal Use: A single leading underscore is a hint to developers, indicating that the object should be treated as 'protected' and not accessed directly from outside the class/module.
python
1class Example:
2    def __init__(self):
3        self._internal_var = 42
4    
5    def _helper_method(self):
6        return "Helper Method"
7
8example = Example()
9print(example._internal_var)  # 42 (accessible but discouraged)
  • Avoiding Conflicts: In some situations, especially when importing from modules, a single underscore can be useful.
python
1# In module_a.py
2_internal_var = "This is meant to be private"
3
4# In another module
5from module_a import *
6
7print(_internal_var)  # Though accessible, this is not recommended

Importance of Convention

The use of a single underscore is purely conventional and does not prevent access. It serves as a guide for developers to indicate which variables or methods are not intended for public use.

Double Underscore (__)

Meaning and Use Cases

Double underscores signal name mangling, which is a feature used to avoid name conflicts in subclasses. It modifies the name of the variable in a way that makes it harder to create subclasses that accidentally override methods or variables of the superclass.

  • Name Mangling: Python automatically changes the name of a method or attribute to include the class name as a prefix. This prevents accidental access and override when inherited.
python
1class Example:
2    def __init__(self):
3        self.__private_var = 84
4
5example = Example()
6print(example.__private_var)  # Raises AttributeError
7
8# The name is mangled, so we can access it using the mangled name
9print(example._Example__private_var)  # 84
  • Avoiding Overrides in Subclasses:
python
1class Base:
2    def __init__(self):
3        self.__attr = "Base Attribute"
4
5class Sub(Base):
6    def __init__(self):
7        super().__init__()
8        self.__attr = "Sub Attribute"
9
10sub_instance = Sub()
11print(sub_instance._Sub__attr)  # Sub Attribute
12print(sub_instance._Base__attr) # Base Attribute

How Name Mangling Works

The name mangling mechanism transforms the name of the method or attribute by appending an underscore followed by the class name (_ClassName__attribute). This ensures that it can't accidentally be overridden in a subclass unless explicitly intended.

Summary Table

Type of UnderscoreCommon UsesCharacteristics
Single (_)Internal Use, Avoid ConflictsConvention-based, easily accessible, for 'protected' variables/methods, used to manage wildcard imports.
Double (__)Name Mangling, Avoid OverridesName mangling to prevent accidental override, harder to access outside the class, used for 'private' access control.

Additional Considerations

  • Use of Single Underscore (_) As a Throwaway Variable: In Python, it's a common practice to use a single underscore when you want to ignore a value, especially in loops or unpacking.
python
for _ in range(5):
    print("Hello!")  # The loop iterates 5 times, and the index is ignored
  • Leading and Trailing Double Underscores (__method__): These are reserved for special Python-defined identifiers known as "magic methods" or "dunder methods" (e.g., __init__, __str__). They have special meanings and are not related to the privacy mechanisms discussed above.
  • Python's Philosophy: According to the Zen of Python by Tim Peters, "Explicit is better than implicit." Utilizing underscores for privacy and naming conventions fits this philosophy, providing a clear, albeit non-enforced, signal to other developers about how code components should be used.

Understanding these naming conventions is fundamental in maintaining clean, understandable, and maintainable Python code, especially when collaborating on larger projects or contributing to shared codebases.


Course illustration
Course illustration

All Rights Reserved.