- Courses
- 💻Programming Languages
- 🐍Python
- Object‑Oriented Programming (OOP)
- Encapsulation
On this page
Encapsulation
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Understanding Encapsulation
Encapsulation is the object‑oriented principle that bundles data (attributes) and behavior (methods) together while controlling how that data is accessed and modified.
Its purpose is to:
- Protect internal object state
- Prevent accidental misuse
- Provide clear, stable interfaces
- Reduce coupling between components
Encapsulation ensures that an object manages its own data responsibly.
Public and Non‑Public Attributes
Python does not enforce strict access control like some languages, but it uses naming conventions to signal intent.
Public Attributes
Public attributes are intended for external access.
class Person:
def __init__(self, name):
self.name = name # public
Public attributes form part of the class’s external interface.
Non‑Public Attributes (Single Underscore)
A leading underscore indicates that an attribute is internal and should not be accessed directly from outside the class.
class Person:
def __init__(self, name, age):
self.name = name
self._age = age # non‑public
This is a convention, not a restriction. It communicates:
“This is internal. Use methods to interact with it.”
Name‑Mangling for Stronger Protection (Double Underscore)
A double underscore triggers name‑mangling, making accidental access harder.
class BankAccount:
def __init__(self, balance):
self.__balance = balance
Internally, Python renames it to _BankAccount__balance.
This discourages external access and protects critical data.
Using Methods to Control Access
Encapsulation encourages interacting with object state through methods, not direct attribute access.
Example:
class BankAccount:
def __init__(self, balance=0):
self.__balance = balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
return self.__balance
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
return self.__balance
return "Insufficient funds"
The class controls:
- How balance is changed
- What operations are allowed
- Validation of inputs
This prevents inconsistent or invalid states.
Getters and Setters (Pythonic Approach)
In Python, getters and setters are implemented using properties, which provide controlled access while keeping attribute syntax clean.
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if value:
self._name = value
Usage:
p = Person("Alice")
p.name = "Bob" # setter called
p.name # getter called
Properties allow:
- Validation
- Computed attributes
- Read‑only attributes
- Backward‑compatible API changes
All while preserving simple attribute access.
Encapsulation and Class Design
Encapsulation supports clean, maintainable design by:
- Hiding implementation details
- Exposing only what is necessary
- Allowing internal changes without breaking external code
- Preventing misuse of internal state
- Encouraging clear, stable interfaces
Well‑encapsulated classes are easier to understand, test, and extend.
Encapsulation vs. Information Hiding
Python’s philosophy emphasizes responsibility over restriction:
- Encapsulation organizes and protects state
- Information hiding discourages direct access but does not forbid it
- Developers are trusted to respect conventions
This flexibility makes Python expressive while still supporting strong OOP design.
Summary
In this lesson, you learned how encapsulation protects and organizes object state. You explored public and non‑public attributes, name‑mangling, controlled access through methods, and Pythonic properties. Encapsulation is essential for designing robust, maintainable classes that expose clear interfaces while safeguarding their internal behavior.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Polymorphism
Next
Magic Methods
-
Understanding Encapsulation
-
Public and Non‑Public Attributes
- Public Attributes
- Non‑Public Attributes (Single Underscore)
- Name‑Mangling for Stronger Protection (Double Underscore)
-
Using Methods to Control Access
-
Getters and Setters (Pythonic Approach)
-
Encapsulation and Class Design
-
Encapsulation vs. Information Hiding
-
Summary
