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 Python1. Public Access ModifierMembers (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() OutputName: 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 ModifierA 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() OutputName: 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) OutputName: R2J Roll: 1706256 Branch: CSE R2J Explanation:__name, __roll, __branch: private variables__displayDetails(): private methodDirect access from outside will raise AttributeErrorAccess 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) Related Articles:OOPs in PythonEncapsulation in PythonName Mangling in Python Create Quiz Comment R riturajsaha Follow 82 Improve R riturajsaha Follow 82 Improve Article Tags : Python python-oop-concepts Python-OOP Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like