Why are Python's 'private' methods not actually private?
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.
Introduction
Python is renowned for its simplicity and readability, qualities that attract both beginners and seasoned developers alike. However, these traits can sometimes lead to misunderstandings about the language's more nuanced features. One common point of confusion is the concept of "private" methods. In many programming languages, you can explicitly define methods as private, ensuring strict encapsulation. In Python, however, this is not the case. Although Python offers a mechanism that mimics this behavior through name mangling, the truth is that Python’s "private" methods are not actually private.
Understanding Encapsulation
Before diving into Python's specifics, it's essential to understand why encapsulation is desirable in programming. Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP). It involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit or class. Concealing the internal state of the object from the outside world creates a clear interface contract and reduces the chance for unintentional interference.
In languages like Java or C++, private methods and attributes are strictly inaccessible outside their class. Attempting to access them will result in a compilation error. This enforcement at the language level assures developers that certain parts of their codebase remain untouched and unaltered.
Python's Approach to Privacy
Name Mangling
Python takes a more relaxed approach to privacy, following the "we are all consenting adults here" philosophy. Instead of private method enforcement, Python uses name mangling for methods intended to be private. By prefixing a method or attribute name with two underscores, Python alters its name to include the class name, like so: __method
becomes _ClassName__method
.
- Use Single Underscore by Convention: While not enforced by the interpreter, Python follows a convention of prefixing an attribute or method with a single underscore (
_) to indicate that it is intended for internal use only. - Document Intent: Use comments and documentation strings to elucidate the intended privacy level of methods and attributes.
- Consider Property Decorators: If a method is crucial to maintain as immutable, consider using property decorators that add a layer of abstraction.
Related reading
- Why C structs cannot be inherited?
- Why CQRS and Event Sourcing design pattern is getting popular recently?
- Why do Python classes inherit object?
- Why do Python classes inherit object?
- Why are scripting languages e.g. Perl, Python, and Ruby not suitable as shell languages?
- Why are there no and -- operators in Python?
- Why does does it really? ListT implement all these interfaces, not just IListT?
- Why doesn't C infer my generic types?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Object-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.