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.
- Avoiding Conflicts: In some situations, especially when importing from modules, a single underscore can be useful.
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.
- Avoiding Overrides in Subclasses:
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 Underscore | Common Uses | Characteristics |
Single (_) | Internal Use, Avoid Conflicts | Convention-based, easily accessible, for 'protected' variables/methods, used to manage wildcard imports. |
Double (__) | Name Mangling, Avoid Overrides | Name 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.
- 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.

