🔒 Access Specifiers
Last Updated: 05 Sept 2025
Access Specifiers define how variables/methods of a class can be accessed (inside or outside the class).
Unlike Java/C++, Python does not have strict private/public keywords.
👉 Instead, it follows naming conventions:
- Public → Default (can be accessed anywhere)
- Protected → Prefix with
_(single underscore) - Private → Prefix with
__(double underscore)
✏ 1. Public Members
- Accessible from anywhere.
- Default in Python.
class Student:
def __init__(self, name):
self.name = name # public
s1 = Student("Amit")
print(s1.name) # ✅ Accessible
✏ 2. Protected Members(_variable)
- Intended to be used within class or subclasses.
- Just a convention (Python won’t stop you from accessing).
class Student:
def __init__(self, name, age):
self._age = age # protected
s1 = Student("Neha", 22)
print(s1._age) # ⚠️ Works, but not recommended (convention)
✏ 3. Private Members(__variable)
- Python uses Name Mangling (renames variable internally).
- Cannot be accessed directly from outside.
class Student:
def __init__(self, roll):
self.__roll = roll # private
s1 = Student(101)
# print(s1.__roll) ❌ Error
print(s1._Student__roll) # ✅ Accessible via name mangling
Hinglish Tip 🗣: Private = "locker key" → directly access nahi kar sakte, thoda jugad (name mangling) karna padega.