Encapsulation
Locking Down the Hacker’s Vault
Imagine you’re a hacker storing sensitive credentials. You don’t want everyone to peek inside your vault. You want controlled access and only trusted methods should interact with the data. In Python, this principle is called encapsulation.
Encapsulation is about bundling data and methods together while restricting direct access to the data. It’s the art of protecting information and exposing only what’s necessary.
Why Encapsulation Matters
- Data Protection: Prevents accidental or unauthorized modification of internal state.
- Controlled Access: Expose only safe methods to interact with data.
- Abstraction: Users don’t need to know the internal details just the interface.
- Maintainability: Changes inside the class don’t break external code.
- Real‑World Analogy: Think of an ATM. You don’t access the bank’s database directly - you interact through a secure interface.
Encapsulation in Python
Python doesn’t enforce strict access modifiers like Java or C++. Instead, it uses naming conventions:
- Public: Accessible everywhere (default).
- Protected (
_variable): Intended for internal use, but still accessible. - Private (
__variable): Name‑mangled to discourage direct access.
Public, Protected & Private
class Account:
def __init__(self, owner, balance):
self.owner = owner # public
self._balance = balance # protected
self.__pin = "1234" # private
def show_balance(self):
print(f"{self.owner}'s balance: {self._balance}")
def verify_pin(self, pin):
if pin == self.__pin:
print("Access granted.")
else:
print("Access denied.")
acc = Account("Shubham", 5000)
print(acc.owner) # Public access
print(acc._balance) # Accessible, but discouraged
# print(acc.__pin) # Error: private attribute
acc.verify_pin("1234") # Access granted
- Why? Encapsulation protects sensitive data (
__pin) while still allowing controlled access through methods.
Getter & Setter Methods
class Student:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_name(self, name):
if len(name) > 0:
self.__name = name
else:
print("Invalid name.")
s = Student("Aditi")
print(s.get_name()) # Aditi
s.set_name("Shubham")
print(s.get_name()) # Shubham
- Why? Getters and setters provide controlled access, ensuring data integrity.
Real‑World Example
class APIKey:
def __init__(self, key):
self.__key = key
def authenticate(self, provided_key):
return provided_key == self.__key
api = APIKey("SECRET123")
print(api.authenticate("SECRET123")) # True
print(api.authenticate("WRONG")) # False
- Why? Encapsulation ensures the key is hidden and only verified through a secure method.
The Hacker’s Notebook
- Encapsulation bundles data and methods, restricting direct access. Python uses naming conventions (
_protected,__private) to signal access levels. - Getter and setter methods provide controlled, validated access. Encapsulation protects sensitive data like PINs, keys, or credentials.
Hacker’s Mindset: treat encapsulation as your vault security system. Expose only what’s safe, protect what’s sensitive, and control access with precision.

Updated on Jan 3, 2026