Access Modifiers:

Access modifiers are used to restrict the method or variable to unauthorized access by using the single underscore and double underscore.

Types of Access Modifiers:

  1. Public
  2. Protected
  3. Private

Public Members:

  • It can be accessed into or outside of the class.

Protected Members:

Protected class can be accessed from the class and from sub class, but can not call from outside of the class simply use ‘_’ to make the method Protected.

Private Members:

Private calls from only self class neither it call from subclass nor outside of the class.
‘__’ used to make the method Private.

Example

class Info:
        def __init__(self,name,month,post):
            self.name = name
            self._monthly = month
            self.__post = post
        
        def display(self):
            print('Name is',self.name)
            print('Monthly salary is Rs.',self._monthly) 
            print('Post is',self.__post)

in_1 = Info("Ramsaran",10000,"Sales Manager")
in_1.display()
print(in_1.name)
print(in_1.monthly)

Output:

Python 3.8.4 (tags/v3.8.2:7b3acc59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32

>>> 
================== RESTART: C:/Users/PC/Desktop/Hello.py ================
Name is Ramsaran
Monthly salary is Rs. 10000
Post is Sales Manager
Ramsaran
Traceback (most recent call last):
  File "C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/oops.py", line 16, in 
    print(in_1.post)
AttributeError: 'Info' object has no attribute 'monthly'

Here raise the Attribute error due the restrict the method monthly.
Similarly now we print post variable

class Info:
    ''
    def __init__(self,name,month,post):
        self.name = name
        self._monthly = month
        self.__post = post
    
    def display(self):
        print('Name is',self.name)
        print('Monthly salary is Rs.',self._monthly) 
        print('Post is',self.__post)

in_1 = Info("Ramsaran",10000,"Sales Manager")
in_1.display()
print(in_1.name)
print(in_1.post)

Output:

Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32

>>> 
================== RESTART: C:/Users/PC/Desktop/Hello.py ================
Name is Ramsaran
Monthly salary is Rs. 10000
Post is Sales Manager
Ramsaran
Traceback (most recent call last):
  File "C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/oops.py", line 16, in 
    print(in_1.post)
AttributeError: 'Info' object has no attribute 'post'

Now we can see that we cannot use Private or Protected Member from the outside of the class now we will access the following code using some method.

class Information:
    
    def __init__(self,name,month,post):
        self.name = name
        self._monthly = month
        self.post = post
    
    def basic_info(self):
        print('========Basic Detail===========')
        print('Name is',self.name)
        print('Post is',self.post)

    def gMonthly(self):
        print(f'{self.name} salary is Rs.{self._monthly}pm')
        
    def sMonthly(self,value):
        if self.post=='Manager':
            print('New pm salary is set to',value)
            self._monthly= value
        else:
            print('Access Restricted')

emp_1 = Information("Pooja",200000,"Govt Off.")
emp_1.gMonthly()
emp_1.sMonthly(28000)
emp_1.gMonthly()

# But can access and modify using other ways
print('Modify using dot notation')
emp_1.monthly = 28000
emp_1.gMonthly()
print(vars(emp_1))

Output:

Python 3.8.4 (tags/v3.8.2:7b3cb59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32

>>> 
================== RESTART: C:/Users/PC/Desktop/Hello.py ================
Pooja salary is Rs.200000pm
Access Restricted
Pooja salary is Rs.200000pm
Modify using dot notation
Pooja salary is Rs.200000pm
{'name': 'Pooja', '_monthly': 200000, 'post': 'Govt Off.', 'monthly': 28000}
>>> 
  • So the actual monthly salary of employees remains the same.
  • But also another public attribute with name monthly added in the attribute dictionary of object, which is ambiguous.
  • This is where the property method in Python comes to rescue.
Subscribe Now