Open In App

Access Modifiers in Python : Public, Private and Protected

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
82 Likes
Like
Report

Access modifiers in Python control which parts of a class can be accessed from outside the class, from within the class, or by subclasses. They help keep data and methods safe and organized.

Types of Access Modifiers in Python

1. Public Access Modifier

  • Members (variables or methods) declared as public can be accessed from anywhere in the program.
  • By default, all members are public in Python.

Example:

Python
class Geek: def __init__(self, name, age): self.geekName = name self.geekAge = age def displayAge(self): print("Age:", self.geekAge) obj = Geek("R2J", 20) print("Name:", obj.geekName) obj.displayAge() 

Output
Name: R2J Age: 20 

Explanation:

  • geekName and geekAge are public variables, so they can be accessed directly from outside the class (obj.geekName).
  • displayAge() is a public method, so it can be called normally using obj.displayAge().
  • In Python, class members are public by default, so both the variables and the method will appear when you check dir(obj).

2. Protected Access Modifier

  • A member is considered protected if its name starts with a single underscore (_).
  • Convention only: It suggests that the member should not be accessed outside the class except by subclasses.
  • Still, Python allows direct access if explicitly called.

Example:

Python
class Student: def __init__(self, name, roll, branch): self._name = name self._roll = roll self._branch = branch def _displayRollAndBranch(self): print("Roll:", self._roll) print("Branch:", self._branch) class Geek(Student): def displayDetails(self): print("Name:", self._name) self._displayRollAndBranch() obj = Geek("R2J", 1706256, "IT") obj.displayDetails() 

Output
Name: R2J Roll: 1706256 Branch: IT 

Explanation:

  • _name, _roll, and _branch are protected members, meant to be used within the class and subclasses.
  • _displayRollAndBranch() is a protected method, accessed by the subclass Geek.
  • Python allows direct access (e.g., obj._name), but by convention, it’s avoided.

Private Access Modifier:

  • A member is private if its name starts with double underscores (__).
  • Python does not enforce strict privacy — instead, it uses Name Mangling.
  • The interpreter renames __var → _ClassName__var internally.

Example:

Python
class Geek: def __init__(self, name, roll, branch): self.__name = name self.__roll = roll self.__branch = branch def __displayDetails(self): print("Name:", self.__name) print("Roll:", self.__roll) print("Branch:", self.__branch) def accessPrivateFunction(self): self.__displayDetails() obj = Geek("R2J", 1706256, "CSE") obj.accessPrivateFunction() print(obj._Geek__name) 

Output
Name: R2J Roll: 1706256 Branch: CSE R2J 

Explanation:

  • __name, __roll, __branch: private variables
  • __displayDetails(): private method
  • Direct access from outside will raise AttributeError
  • Access allowed inside the class or via name mangling (obj._Geek__name)Combined

Example of All Access Modifiers

Python
class Super: publicData = "Public Data Member" _protectedData = "Protected Data Member" __privateData = "Private Data Member" def accessPrivateMembers(self): print("Accessing inside class:", self.__privateData) class Sub(Super): def accessProtectedMembers(self): print("Accessing inside subclass:", self._protectedData) obj = Sub() # Public → Direct Access print(obj.publicData) # Protected → Accessible (but discouraged) print(obj._protectedData) # Private → Not directly accessible # print(obj.__privateData) AttributeError obj.accessPrivateMembers() # Using Name Mangling print(obj._Super__privateData) 

Explore