Use a leading underscore to mark a class member as non-public in Python. Privacy in Python is largely a convention: callers can still access the name. A double leading underscore adds name mangling, which helps prevent accidental clashes with subclasses but does not enforce access control.
Python Private Names and a Small Example
Run this code in a Python interpreter:
class Counter:
def __init__(self):
self._value = 0
self.__step = 2
def advance(self):
self._value += self.__step
return self._value
counter = Counter()
print(counter.advance()) # 2
print(counter._value) # 2
print(counter._Counter__step) # 2
# counter.__step # raises AttributeError
The public method advance() is the intended interface. _value uses one underscore to signal an implementation detail. Python does not block the final caller from reading or changing it; doing so couples that caller to an internal detail that the class author may change.
The name __step is rewritten within the class definition as _Counter__step. A normal lookup of counter.__step outside the class therefore looks for a different name and raises AttributeError in this example. Looking up the mangled spelling works. The value is stored on the instance even though its mangled name includes the class name; the prefix does not make it a shared class attribute.
Why Mangle a Name?
A Python private class member with two leading underscores is useful when a base class and a subclass might otherwise use the same internal name. The transformation incorporates the class where the identifier is written:
class SpecialCounter(Counter):
def __init__(self):
super().__init__()
self.__step = 10
item = SpecialCounter()
print(item.advance()) # 2
print(item._Counter__step) # 2
print(item._SpecialCounter__step) # 10
The inherited method uses _Counter__step; the subclass assignment uses _SpecialCounter__step. They coexist. This reduces accidental collisions without preventing a deliberate access. Use the convention when inheritance makes that distinction valuable. A single underscore is usually clearer for ordinary non-public members that subclasses may reasonably need to use.
Names with leading and trailing double underscores, such as __init__, have special roles and are not treated like __step. Do not invent special-method names for application internals. The name-mangling rule applies in a class definition; it does not automatically rewrite a string passed to getattr(). For the example above, getattr(counter, "_Counter__step") retrieves the value, whereas the string "__step" does not find that attribute.
Design the Boundary Explicitly
Private attributes in a Python class communicate intended use, not a security boundary. Code with access to the object can inspect or alter the mangled attribute. Do not place a secret in an object and rely on underscores to keep it from other code running with the same access. Use appropriate process, service, or permission boundaries for actual isolation.
For a maintainable Python class, mark private implementation details consistently and document the public operations. If callers need to inspect a value without directly depending on storage, expose a property or method with a defined contract. Tests can exercise that contract; direct inspection of internal names is sometimes helpful for debugging, but it makes tests more sensitive to internal changes.
When choosing private names in Python classes, consider who may extend the class and which methods are supported for callers. A subclass collision is a reason to consider double underscores. Otherwise, a single underscore states the boundary with less surprise.
Sources: Python tutorial: private variables and Python reference: private name mangling, accessed November 20, 2026.
