Protected Access Modifier
In Python, the protected access modifier isn't explicitly supported as it is in languages like Java or C++. However, Python follows a convention to make an attribute protected by prefixing it with a single underscore (_). This convention tells other programmers that the attribute should be treated as protected, meaning it should not be accessed from outside the class, subclass, or module unless for a good reason.
Example of Protected Access Modifier
Let’s see how this works with an example:
Python
class Robot:
def __init__(self, name, functionality):
self._name = name # Protected attribute
self._functionality = functionality # Protected attribute
def display_info(self):
print(f"Robot Name: {self._name}")
print(f"Functionality: {self._functionality}")
class ServiceRobot(Robot):
def __init__(self, name, functionality, service_area):
super().__init__(name, functionality)
self.service_area = service_area # Public attribute
def display_service_info(self):
print(f"Service Area: {self.service_area}")
self.display_info() # Accessing protected method from the parent class
# Creating an instance of ServiceRobot
my_service_robot = ServiceRobot("R2-D2", "Astromech", "Engineering")
# Accessing protected attributes through a subclass
my_service_robot.display_service_info()

You can also try this code with Online Python Compiler
Run Code
Output
Service Area: Engineering
Robot Name: R2-D2
Functionality: Astromech
In this code, _name and _functionality are intended as protected. They can be accessed within their class and by subclasses, like ServiceRobot. The underscore does not prevent access technically but is a strong hint to the developer that these should not be touched directly outside of class methods and subclasses.
Private Access Modifier
Private access modifiers are crucial in Python for restricting access to certain attributes or methods within a class, making them only callable from within the class itself. This is enforced through naming conventions similar to the protected access, but stronger. In Python, you denote private attributes by prefixing them with double underscores (__).
Example of Private Access Modifier
Here’s an example to demonstrate how private attributes work in Python:
Python
class Robot:
def __init__(self, name, functionality):
self.__name = name # Private attribute
self.__functionality = functionality # Private attribute
def display_info(self):
print(f"Robot Name: {self.__name}")
print(f"Functionality: {self.__functionality}")
# Trying to access private attributes from outside the class will lead to an error
my_robot = Robot("R2-D2", "Astromech")
# The following lines will throw an error
# print(my_robot.__name)
# print(my_robot.__functionality)
my_robot.display_info() # This method can access the private attributes and works fine

You can also try this code with Online Python Compiler
Run Code
Output
Robot Name: R2-D2
Functionality: Astromech
In the above code, __name and __functionality are private attributes. They are only accessible within the Robot class, specifically within its methods. If you try to access these attributes directly from outside the class, Python will raise an AttributeError, indicating that the attribute is not accessible. This ensures that sensitive data is hidden and protected from external interference, a practice that is very useful in larger software projects where maintaining data integrity is crucial.
Frequently Asked Questions
What are access modifiers in Python?
Python doesn't have built-in access modifiers. Instead, it uses naming conventions like underscores to indicate method and variable accessibility.
What are the 4 access modifiers?
In many programming languages, the four access modifiers are public, private, protected, and default (no modifier specified).
Why doesn't Python have access modifiers?
Python follows a philosophy of "we're all consenting adults here," promoting less restrictive coding practices and relying on convention over enforcement.
Conclusion
In this article, we have learned about the different types of access modifiers in Python: public, protected, and private. We explored how public access allows attributes to be used freely throughout the code, while protected access uses a naming convention to hint at more restricted use within classes and their subclasses. Private access further secures attributes by restricting their access strictly to their own class methods, ensuring critical data remains protected from external manipulation.
You can refer to our guided paths on the Code360. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.