Access Modifiers in Python : Public, Private and Protected


Access modifiers are used by object oriented programming languages like C++,java,python etc. to restrict the access of the class member variable and methods from outside the class. Encapsulation is an OOPs principle which protects the internal data of the class using Access modifiers like Public,Private and Protected.

Python supports three types of access modifiers which are public,private and protected. These access modifiers provide restrictions on the access of member variables and methods of the class from any object outside the class.

Public Access Modifier

By default the member variables and methods are public which means they can be accessed from anywhere outside or inside the class. No public keyword is required to make the class or methods and properties public.Here is an example of Public access modifier −

Example

The student class has two member variables, name and age and a method display which prints the member variable values.Both these variables and the methods are public as no specific keyword is assigned to them.

class Student:
   def __init__(self, name, age):
      self.name = name
      self.age = age
    
   def display(self):
      print("Name:", self.name)
      print("Age:", self.age)

s = Student("John", 20)
s.display() 

Output

Name: John
Age: 20

Private Access Modifier

Class properties and methods with private access modifier can only be accessed within the class where they are defined and cannot be accessed outside the class. In Python private properties and methods are declared by adding a prefix with two underscores(‘__’) before their declaration.

Example

The Class BankAccount is being declared with two private variables i.e account_number and balance and a private property display_balance which prints the balance of the bank account. As both the properties and method are private so while accessing them from outside the class it raises Attribute error.

class BankAccount:
   def __init__(self, account_number, balance):
      self.__account_number = account_number
      self.__balance = balance
    
   def __display_balance(self):
      print("Balance:", self.__balance)

b = BankAccount(1234567890, 5000)
b.__display_balance() 

Output

AttributeError: 'BankAccount' object has no attribute '__display_balance'

Protected Access Modifier

Class properties and methods with protected access modifier can be accessed within the class and from the class that inherits the protected class. In python, protected members and methods are declared using single underscore(‘_’) as prefix before their names.

Example

The Person class has two protected properties i.e _name and _age and a protected method _display that displays the values of the properties of the person class. The student class is inherited from Person class with an additional property i.e _roll_number which is also protected and a public method display that class the _display method of the parent class i.e Person class by creating an instance of the Student class we can call the display method from outside the class as the display method is private which calls the protected _display method of Person class.

class Person:
   def __init__(self, name, age):
      self._name = name
      self._age = age
    
   def _display(self):
      print("Name:", self._name)
      print("Age:", self._age)

class Student(Person):
   def __init__(self, name, age, roll_number):
      super().__init__(name, age)
      self._roll_number = roll_number
    
   def display(self):
      self._display()
      print("Roll Number:", self._roll_number)

s = Student("John", 20, 123)
s.display() 

Output

Name: John
Age: 20
Roll Number: 123

Conclusion

In this article we understood the three access modifiers which is used python and other OOPs languages for data hiding and protection.Public, private and protected are three types of access modifiers which are used in python. Public properties and methods of a class can be accessed from anywhere outside or inside the class. Private member variables and properties can only be accessed from inside the class in which the properties and methods are declared. Protected access modifiers are used when we need to access the properties and methods of the class from inside the class and from the class which inherits from that class.

Updated on: 13-Apr-2023

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements