Access Modifiers

Access Modifiers

Through object-oriented programming, Python gives us the ability to control Access Modifiers which allow us to restrict the access of selected variables and functions within a class or an object. There are 3 Access Modifiers that we can use and they are common for most programming languages, they are:

  • Public
  • Protected
  • Private

Access Modifiers play a key role in security by allowing us to secure data from unauthorized access. In Python, we use an underscore ‘_’ to modify access specifiers.

Public

Any or all variables and functions that are Public can be accessed from any part of the program. By default, all variables and functions of a class are Public, all the classes we wrote in the previous blogs have Public Access Modifiers.

class Subject:

    # constructor
    def __init__(self, subject_name, subject_teacher):
        # public variables
        self.subject_name = subject_name
        self.subject_teacher = subject_teacher

    # public function
    def display_details(self):
        print(self.subject_name, "teaches", self.subject_teacher)


object_1 = Subject("Physics", "Rose")
print(object_1.subject_name)  # Prints Physics
print(object_1.subject_teacher)  # Prints Rose
object_1.display_details()  # Prints 'Rose teaches Physics'

Protected

When we declare a variable or a function to be protected, it can only be accessed by an object of its class or an object of its sub-class. To make a variable or a function Protected all we have to do is add an underscore before it.

class Subject:

    # constructor
    def __init__(self, subject_name, subject_teacher):
        # protected variables
        self._subject_name = subject_name
        self._subject_teacher = subject_teacher

    # protected function
    def _display_details(self):
        print(self._subject_name, "teaches", self._subject_teacher)


object_1 = Subject("Physics", "Rose")
print(object_1._subject_name)  # Prints Physics
print(object_1._subject_teacher)  # Prints Rose
object_1._display_details()  # Prints 'Rose teaches Physics'

Private

When we declare a variable or a function as Private it can only be accessed within the class. If an object tries to access a Private variable or function it will raise an error. To declare a variable or function as Private we need to add two underscores.

class Subject:

    # constructor
    def __init__(self, subject_name, subject_teacher):
        # private variables
        self.__subject_name = subject_name
        self.__subject_teacher = subject_teacher

    # private function
    def __display_details(self):
        print(self.__subject_name, "teaches", self.__subject_teacher)

    # public function
    def call_private_function(self):
        self.__display_details()


object_1 = Subject("Physics", "Rose")
print(object_1.__subject_name)  # Prints an error
print(object_1.__subject_teacher)  # Prints an error
object_1.__display_details()  # Prints an error
object_1.call_private_function()  # Prints 'Rose teaches Physics'

 

Only the last line of code will run without an error, as we are calling a Public function within a class that class a Private function. In other words, we can access the function “call_private_function” as it is Public and in the same class as the Private function called “display_details”.

 

What have we learned?

  • What are Access Modifiers?
  • What are the three Access Modifiers in Python?
  • Which is the default Access Modifier in Python?
  • What do we use to modify access specifiers?
  • What advantage do we have using Access Modifiers?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments