Why Does Python Use a Leading Underscore for Some Variables?
The article explains that a leading underscore in Python names signals a non‑public or internal use convention defined by PEP 8, describes its practical effects on imports and IDE hints, and outlines other underscore naming patterns such as name mangling, trailing underscores, magic methods, and throwaway variables.
Python does not have explicit private or protected keywords; instead, the community follows the PEP 8 convention that a single leading underscore (e.g., _var) marks a name as non‑public or intended for internal use. This is merely a convention, not a language‑enforced rule, so the attribute can still be accessed from outside (e.g., obj._age) without error, but doing so is discouraged because future changes to the internal implementation may break external code.
The convention has two concrete effects: (1) names starting with an underscore are omitted when a module is imported with from module import *; (2) many IDEs (VS Code, PyCharm, etc.) display such members in a dimmed colour to remind developers they are internal.
Adhering to this naming style improves code readability, making it clear which symbols form the public API and which are internal details—especially important in large projects and team collaborations.
Beyond the single leading underscore, Python uses several other underscore patterns:
Double leading underscore ( __var) triggers name mangling, automatically rewriting the attribute to _ClassName__var to avoid accidental overriding in subclasses. This provides a form of “pseudo‑private” protection. Example:
class Parent:
def __init__(self):
self.__secret = "parent secret"
class Child(Parent):
def __init__(self):
super().__init__()
self.__secret = "child secret" # does not overwrite parent
c = Child()
# print(c.__secret) # AttributeError
print(c._Parent__secret) # Accesses the mangled nameTrailing underscore ( var_) avoids clashes with Python keywords, such as class_, list_, etc.
Double leading and trailing underscores ( __var__) denote magic (dunder) methods like __init__, __str__, __len__. These are reserved for Python’s data model and should not be invented arbitrarily.
Single solitary underscore ( _) is used as a throwaway variable in loops or unpacking when the value is intentionally ignored, e.g.:
for _ in range(5):
print("hello")
a, _, c = (1, 2, 3) # ignore the middle valueIn summary, a leading underscore is a naming convention that signals internal use, improves readability, and aids tooling, while other underscore patterns serve specific purposes such as name mangling, avoiding keyword conflicts, defining special methods, or discarding values.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
