10 Access Modifiers
10 Access Modifiers
In this lesson, you will learn about the private, public, and protected data members in Python.
• Public Attributes
• Private Attributes
• Private Properties
• Code Explanation
• Private Methods
• Code Explanation
• Accessing Private Attributes in the Main Code
• Not So Protected
There are two types of access modifiers in Python. Let’s take a look at them
one by one.
Public Attributes #
Public attributes are those that be can be accessed inside the class and
outside the class.
class Employee:
def __init__(self, ID, salary):
# all properties are public
self.ID = ID
self.salary = salary
def displayID(self):
print("ID:", self.ID)
In the code above, the properties ID and salary , and the method displayID()
are public as they can be accessed in the class as well as the outside the class.
ID
salary
Main
class
Private Attributes #
Private attributes cannot be accessed directly from outside the class but
can be accessed from inside the class.
The aim is to keep it hidden from the users and other classes. Unlike in many
different languages, it is not a widespread practice in Python to keep the data
members private since we do not want to create hindrances for the users. We
can make members private using the double underscore __ prefix
Trying to access private attributes in the main code will generate an error. An
example of this is shown below:
Private Properties #
Let’s see a code example for implementing private properties:
private_properties
class Employee:
def __init__(self, ID, salary):
self.ID = ID
self.__salary = salary # salary is a private property
Code Explanation #
In the code above, ID is a public property but __salary is a private
property, so it cannot be accessed outside the class.
To ensure that no one from the outside knows about this private property,
the error does not reveal the identity of it.
'Employee'
object has no Calling
Employee attribute
'__salary'
Employee to
access __salary
ID
__salary
Main
Class
Private Methods #
Let’s see a code example for implementing private methods:
private_methods
class Employee:
def __init__(self, ID, salary):
self.ID = ID
self.__salary = salary # salary is a private property
Code Explanation #
ID is a public property, so it can be accessed from outside and inside the
class.
__salary is a private property, so it cannot be accessed from outside the
class but can be accessed from inside the class.
To ensure that no one from the outside knows about this private property,
the error does not reveal the identity of it.
'Employee'
calling
object has no
Employee to
Employee attribute
'__displayID()'
access
__displayID()
displaySalary()
__displayID()
ID
__salary Main
Class
Note: Methods are usually public since they provide an interface for the
class properties and the main code to interact with each other.
Properties and methods with __ prefix are usually present to make sure that
the user does not carelessly access them. Python allows for a free hand to the
user to avoid any future complications in the code. If the user believes it is
class Employee:
def __init__(self, ID, salary):
self.ID = ID
self.__salary = salary # salary is a private property
Not So Protected #
Protected properties and methods in other languages can be accessed by
classes and their subclasses which will be discussed later in the course. As we
have seen, Python does not have a hard rule for accessing properties and
methods, so it does not have the protected access modifier.