Access Modifiers in Python are used to determine the access of the variables and methods of a class into other classes. Most of object oriented programming languages like C++, Java, Python are use three types of access specifier Public, Protected and Private.
In Python ‘_’ symbol is used to determine the access control for a specific variable(Data Member) or a Method(Member function) of a Class.
Access specifiers in Python helps to secure data of a class and preventing it from being accessible from unsecure sources and from unauthorized access.
A Python Class has three types of access modifier.
Public
Protected
Private
Access Modifiers in Python
1. PublicAccess Modifier
In Python, by default every data members and member functions are public.
We can access Public members from any part of the program i.e. within same class or from outside of the class.
#public access modifier in a class
class Student:
def __init__(self, name, age):
#public data members
self.name = name
self.age = age
#public member function
def displayStudentDetails(self):
# accessing public data member
print("\n--- Student Details ---")
print("Name = ", self.name)
print("Age = ", self.age)
#object of the class
s1 = Student("Sita", 22)
#accessing public data member
print("Student Name = ", s1.name)
# public member function call of the class
s1.displayStudentDetails()
OUTPUT
Student Name = Sita
--- Student Details ---
Name = Sita
Age = 22
In the above example, name and age of Student class are public data members and displayStudentDetails() method of Student class is a public member function. These public members of the Student class can be accessed from anywhere in the program.
2. ProtectedAccess Modifier
In Python Class, Protected members can be accessible anywhere within class but from outside the classes it can only be accessible by child classes.
We can specify data members and member functions of a class as protected by prefixing with ‘_’ (single underscore) symbol.
#protected access modifier in a class
#super class
class Person:
#protected data members
_name = None
_age = None
_city = None
def __init__(self, name, age, city):
self._name = name
self._age = age
self._city = city
# protected member function
def _displayPersonDetails(self):
# accessing protected data members
print("Age = ", self._age)
print("City = ", self._city)
#child class
class Student(Person):
def __init__(self, name, age, city):
Student.__init__(self, name, age, city)
# public member function
def displayDetails(self):
# accessing protected data members of super class
print("Name = ", self._name)
# accessing protected member functions of super class
self._displayPersonDetails()
# objects of the child class
s1 = Student("Shyam", 22, "Ahmedabad")
# public member functions call of the Student class
s1.displayDetails()
OUTPUT
Name = Shyam
Age = 22
City = Ahmedabad
In the above example, _name, _age and _city are protected data members of Person class and _displayPersonDetails() method is a protected method of the super class Person.
Here, Student class is child class of the Person class so student class can access protected members of Person class. The displayDetails() method of Student class is a public member function which accesses the protected data members and member function of the Person class.
3.Private Access Modifier
In Python Class, Private members of a class are accessible within the class only, private access modifier is the most secure access modifier.
We can specify Data members of a class are private by prefixing ‘__'(Double underscores) symbol.
#private access modifier in a class
class Student:
#private data members
__name = None
__age = None
def __init__(self, name, age):
#private data members
self.__name = name
self.__age = age
#private member function
def __displayStudentDetails(self):
# accessing public data member
print("\n--- Student Details ---")
print("Name = ", self.__name)
print("Age = ", self.__age)
# public member function
def accessDisplayFunction(self):
# accesing private member function
self.__displayStudentDetails()
#object of the class
s1 = Student("Radha", 20)
# public member function call of the class
s1.accessDisplayFunction()
OUTPUT
--- Student Details ---
Name = Radha
Age = 20
In the above example, __name and __age are private members of Student class, __displayStudentDetails() method is a private member function which can only be accessed within the Student class and accessDisplayFunction() method is a public member function of the Student class which can be accessed from anywhere within the program.
Here, The accessDisplayFunction() method accesses the private method __displayStudentDetails() of the student class which prints private data members of class.
Mangling In Python
In Python Class, we can say that there is no existence of “Private” member variables that cannot be accessed except inside a class. We can’t access private variables directly but from outside of the class we can access indirectly.
In Python, leading double underscore tell python interpreter to rewrite name in order to avoid conflict in subclass.
Python Interpreter changes variable name with class name extension and that is known as the Mangling.
i.e. we have a __name private data member , python interpreter replaced private member __name with _classname__name, where classname is the current class name with a prefix underscore.
#mangling in python
class Student:
def __init__(self):
#public data members
self.__name = "Shivam"
self.__age = 19
#object of the class
s1 = Student()
#Access private members of the class
print("Name = ", s1._Student__name)
print("Age = ", s1._Student__age)
OUTPUT
Name = Shivam
Age = 19
In above example we have __name and __age private data members of student class. We are accessing both the members using s1._Student__name and s1._Student__age indirectly.