Single vs Double Leading Underscore in Python

In Python, the difference between a single leading underscore (_var) and double leading underscores (__var) comes down to convention versus interpreter-level enforcement. A single leading underscore serves as a stylistic hint to developers that a variable or method is intended for internal use, while double leading underscores invoke name mangling, where the Python interpreter dynamically rewrites the attribute name to prevent naming collisions in inheritance hierarchies.

Single Leading Underscore (_variable)

A single underscore before a name is a standard PEP 8 naming convention indicating that an attribute, function, or method is intended to be private or internal to its containing module or class.

Double Leading Underscore (__variable)

Double leading underscores trigger name mangling. Name mangling is a syntactic feature enforced by the Python interpreter to help prevent subclasses from accidentally overriding parent class attributes and methods.

Summary of Differences