We can impose access restrictions on different data members and member functions. The restrictions are specified via access modifiers. Access modifiers are levels of hidden information hiding specified for each data member or method defined in the class.
There are 3 types of access modifiers in python:
- Public
- Protected
- Private
Public
Public attributes are those that be can be accessed inside the class and outside the class.
In python, all the attributes are public by default, if you want to change the access level you have to set it to this attribute.
Below is an example of implementing a public access modifier:
class Vehicle:
def __init__(self, name, speed):
self.name = name #public data member
self.speed=speed #public data member
def display_name(self):#public method
print("name:", self.name)
my_vechile = Vehicle("USS Nautilus", 3000)
my_vechile.display_name()
#Output
name: USS Nautilus
In the code above, the properties name and speed, and the method display_name are public as they can be accessed in the class as well as outside the class.
Protected
Protected attributes can be accessed through the class they declared in, or its subclasses (derived classes) only, and can be defined by adding a single underscore _ before the class attribute.
Below is a simple implementation for inheritance with protected attributes:
class Vehicle: #Parent class
def __init__(self, name, speed):
self._name = name #protected data member
self._speed=speed #protected data member
def _display_name(self): #protected data member
print("name:", self._name)
class Car(Vehicle): #Child class
def __init__(self, name, speed, year):
super().__init__(name, speed) #used super to point to parent class
self.year=year #public data member
def display_details(self): #protected method
self._display_name()
print("speed: ", self._speed)
print("year:", self.year)
my_car = Car("GMC Hummer EV", 300, 2020)
my_car.display_details()
#Output
name: GMC Hummer EV
speed: 300
year: 2020
In the above code we have a Car class which inherits from the Vehicle class, and as we see the vehicle protected members can be accessed easily from the derived class Car like its own member year.
However, python doesn't fully perform the functionality of the protected modifier. The attribute defined in the above program is accessible outside the class scope. It can also be modified as well.
Private
Private attributes cannot be accessed directly from outside the class but can be accessed from inside the class.
The aim at a private level is to keep data hidden from the users and other classes and can be done by adding double underscore __ before attribute name.
class Vehicle:
def __init__(self, name, speed):
self.__name = name #private data member
self.__speed = speed #private data member
def __display_name(self): #private method
print("name:", self.__name)
def __display_speed(self): #private method
print("speed:", self.__speed)
def display_details(self): #public method
self.__display_name()
self.__display_speed()
my_vehicle = Vehicle("USS Nautilus", 3000)
my_vehicle.display_details()
#Output
name: USS Nautilus
speed: 3000
In the above code we declared the same Vehicle class but with private data members then, tried to access them from inside and outside the class, and found it succeeded when we accessed the name in display_name method, but it failed when we accessed the speed from outside the class.
Top comments (0)