Python Access Modifiers : Public, Private, Protected

Posted by

Python Access modifiers play a key role in programming the help us understand the usage restrictions on the variables or methods they are specified on.

Python does not have traditional access modifiers like public, private, or protected, as seen in other programming languages such as Java or C++. Instead, Python uses name mangling to indicate a private attribute, but this is not enforced by the language and is only a convention.

In Python, attributes and methods that are prefixed with a double underscore (__) are subject to name mangling. This means that their name is modified to include the class name as a prefix, in order to avoid naming conflicts between classes.

Access Modifiers specifies whether we have access of a specific class, variable. They are classified according to the requirement for each module.

public is accessible from outside the class, _protected is meant to be used within the class and its subclasses, and __private is inaccessible from outside the class.

Every modifier is different from each other

There are three kinds of python access modifiers

  • Public
  • Private
  • Protected

Python Access Modifiers Video Tutorial :

In this video tutorial let us try to see the detailed implementation of public, private and protected attributes in python.

Public :

Let’s try to observe the public access specifiers with the help of a example. This specifier is mostly used when you don’t want to provide any limitations on the class or variable declared.

Most of the variables are public if there is no security on access is a concern.

class A:
    a = 5

    def display(self):
        print('Hello World')

create an object for the class so that you can make use of the properties specified inside it.

a1 = A()
a1.display()

output :

Hello World

and now try to access variable

print(a1.A)

output:

5

Private :

Let’s us try to observe private implementation. These private methods and variables are only access within the class and not outside.

This provides a level of security such that not everyone can access the properties of a class. This will be helpful when dealing with the scenarios like online payment and so on..

You can specify a private attribute or method by prefixing it with a double underscore (__).

class A:
    __a = 5

    def display(self):
        print(self.__a)

create an object and call method

a1 = A()
a1.display()

output :

5

Protected :

Protected variables and methods are restricted to only defined class and derived classes so this is a difference between private and protected.

You can make use of this access modifier when you want to enable a level of protection.

You can indicate a protected attribute or method by prefixing it with a single underscore (_).

class A:
    _a = 5

    def _display(self):
        print(self._a)

Create a object and call display method as below.

a1 = A()
a1._display()

output :

5

Name mangling is only meant to prevent accidental name collisions between attributes of different classes. It does not provide true encapsulation or security, as the mangled name can still be accessed if desired. Therefore, it is generally considered good practice in Python to treat all attributes and methods as public, and rely on documentation and naming conventions to indicate their intended usage.


If you have any query’s in this tutorial on python access modifiers do let us know in the comment section below. If you like this tutorial do share us for more interesting updates.

Python Access Modifiers