Prerequisites: Underscore (_) in Python, Private Variables in Python
Various object-oriented languages like C++, Java, Python control access modifications which are used to restrict access to the variables and methods of the class. Most programming languages has three forms of access modifiers, which are Public, Protected and Private in a class.
Python uses ‘_’ symbol to determine the access control for a specific data member or a member function of a class. Access specifiers in Python have an important role to play in securing data from unauthorized access and in preventing it from being exploited.
A Class in Python has three types of access modifiers:
- Public Access Modifier
- Protected Access Modifier
- Private Access Modifier
Public Access Modifier:
The members of a class that are declared public are easily accessible from any part of the program. All data members and member functions of a class are public by default.
Python3
class Geek:
def __init__( self , name, age):
self .geekName = name
self .geekAge = age
def displayAge( self ):
print ( "Age: " , self .geekAge)
obj = Geek( "R2J" , 20 )
print ( "Name: " , obj.geekName)
obj.displayAge()
|
Output:
Name: R2J
Age: 20
In the above program, geekName and geekAge are public data members and displayAge() method is a public member function of the class Geek. These data members of the class Geek can be accessed from anywhere in the program.
Protected Access Modifier:
The members of a class that are declared protected are only accessible to a class derived from it. Data members of a class are declared protected by adding a single underscore ‘_’ symbol before the data member of that class.
Python3
class Student:
_name = None
_roll = None
_branch = None
def __init__( self , name, roll, branch):
self ._name = name
self ._roll = roll
self ._branch = branch
def _displayRollAndBranch( self ):
print ( "Roll: " , self ._roll)
print ( "Branch: " , self ._branch)
class Geek(Student):
def __init__( self , name, roll, branch):
Student.__init__( self , name, roll, branch)
def displayDetails( self ):
print ( "Name: " , self ._name)
self ._displayRollAndBranch()
obj = Geek( "R2J" , 1706256 , "Information Technology" )
obj.displayDetails()
|
Output:
Name: R2J
Roll: 1706256
Branch: Information Technology
In the above program, _name, _roll, and _branch are protected data members and _displayRollAndBranch() method is a protected method of the super class Student. The displayDetails() method is a public member function of the class Geek which is derived from the Student class, the displayDetails() method in Geek class accesses the protected data members of the Student class.
Private Access Modifier:
The members of a class that are declared private are accessible within the class only, private access modifier is the most secure access modifier. Data members of a class are declared private by adding a double underscore ‘__’ symbol before the data member of that class.
Python3
class Geek:
__name = None
__roll = None
__branch = None
def __init__( self , name, roll, branch):
self .__name = name
self .__roll = roll
self .__branch = branch
def __displayDetails( self ):
print ( "Name: " , self .__name)
print ( "Roll: " , self .__roll)
print ( "Branch: " , self .__branch)
def accessPrivateFunction( self ):
self .__displayDetails()
obj = Geek( "R2J" , 1706256 , "Information Technology" )
obj.accessPrivateFunction()
|
Output:
Name: R2J
Roll: 1706256
Branch: Information Technology
In the above program, __name, __roll and __branch are private members, __displayDetails() method is a private member function (these can only be accessed within the class) and accessPrivateFunction() method is a public member function of the class Geek which can be accessed from anywhere within the program. The accessPrivateFunction() method accesses the private members of the class Geek.
Below is a program to illustrate the use of all the above three access modifiers (public, protected, and private) of a class in Python:
Python3
class Super :
var1 = None
_var2 = None
__var3 = None
def __init__( self , var1, var2, var3):
self .var1 = var1
self ._var2 = var2
self .__var3 = var3
def displayPublicMembers( self ):
print ( "Public Data Member: " , self .var1)
def _displayProtectedMembers( self ):
print ( "Protected Data Member: " , self ._var2)
def __displayPrivateMembers( self ):
print ( "Private Data Member: " , self .__var3)
def accessPrivateMembers( self ):
self .__displayPrivateMembers()
class Sub( Super ):
def __init__( self , var1, var2, var3):
Super .__init__( self , var1, var2, var3)
def accessProtectedMembers( self ):
self ._displayProtectedMembers()
obj = Sub( "Geeks" , 4 , "Geeks !" )
obj.displayPublicMembers()
obj.accessProtectedMembers()
obj.accessPrivateMembers()
print ( "Object is accessing protected member:" , obj._var2)
|
Output:
Public Data Member: Geeks
Protected Data Member: 4
Private Data Member: Geeks !
In the above program, the accessProtectedMembers() method is a public member function of the class Sub accesses the _displayProtectedMembers() method which is protected member function of the class Super and the accessPrivateMembers() method is a public member function of the class Super which accesses the __displayPrivateMembers() method which is a private member function of the class Super.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jan, 2022
Like Article
Save Article