A private instance variable is an attribute whose name begins, but does not end, with two underscores. Python mangles that name to reduce accidental access and signal that the attribute belongs to the class’s internal implementation. section 15.9
Private members provide several practical guarantees:
- Restrict ordinary direct access from outside the defining class, supporting security, reliability, and clearer interfaces. section 15.9
- Avoid inheritance name clashes: a subclass and superclass can use the same private name while retaining separate attributes. section 15.9 section 15.11
- Expose internal state through an ordinary method when controlled access is appropriate; deliberate access remains possible by reproducing the mangled name. section 15.9
A private attribute in practice
class Mine: def __init__(self): self.x = 2 self.__y = 3 def print_y(self): print(self.__y) m = Mine() print(m.x) # 2 m.print_y() # 3 print(m.__y) # AttributeError The attribute appears in dir(m) under the mangled name _Mine__y. This is an access barrier against accidents, not an absolute security boundary.











