Access Modifiers Public and Private in Python

In this class, we discuss Access Modifiers Public and Private in Python.

For Complete YouTube Video: Click Here

Access Modifiers

The reader should have prior knowledge of class and instance variables. Click here.

Access modifiers are used to restrict access to variables and methods of the class in python.

We discuss public and private access modifiers in this class.

By default, the variables and methods are given public access.

Public access means. We can access those variables or methods anywhere in the program.

We can access private variables or methods only inside the class.

The private variables or methods are defined with __ before the variable name.

Example:

__var1=20

Suppose a variable or method contains two underscores before the variable name. It is taken as a private variable.

Take an example and understand the concept of access modifiers better.

# understanding public private access modifiers
class pub:
    __z=10  #class private variable
    def __init__(self, value):
        self.__privmem=value # private instance variable
        self.pubmem=value # public instance variable
    def __privclass(self):
        self.__privmem+=pub.__z
        print(self.__privmem)
    def pubclass(self):
        print(self.__privmem)
        self.__privclass()
        print(pub.__z)
x=pub(10)
print(x.pubmem)
x.pubclass()

Output:
10
10
20
10

In the program above, we defined a class pub. And in the class pub, we had a private class variable z.

In the instance variables, we defined one private variable and one public variable.

We defined a private method __privclass and a public method pubclass.

We can use the private variables inside the class. For example, this is shown in the program above.

The object for the class pub is defined and access public variables and methods.

If we access the private variables outside the class. we will get an error message.

The below program is giving an error.

class pub:
    __z=10  #class private variable
    def __init__(self, value):
        self.__privmem=value # private instance variable
        self.pubmem=value # public instance variable
    def __privclass(self): #private method
        self.__privmem+=pub.__z
        print(self.__privmem)
    def pubclass(self):
        print(self.__privmem)
        self.__privclass()
        print(pub.__z)
x=pub(10)
print(x.pubmem)
x.pubclass()
x.__privmem  # private member can not be accessed

Error : 'pub' object has no attribute '__privmem'

Example using Sub Class

class pub:
    __z=10  #class private variable
    def __init__(self, value):
        self.__privmem=value # private instance variable
        self.pubmem=value # public instance variable
    def __privclass(self):# private method
        self.__privmem+=pub.__z
        print(self.__privmem)
    def pubclass(self):
        print(self.__privmem)
        self.__privclass()
        print(pub.__z)
class subclas(pub):
    def __init__(self,value):
        super().__init__(value)
    def pubmeth(self):
        print(self.pubmem)
        print(self.__privmem)
x=subclas(20)
x.pubmeth()

The program shown above is using inheritance and trying to use the private member in the inherited class.

However, there is no access to private members in the inherited class.

Therefore, the above program will give an error.

Analyze the above program for better practice.

Example on private class variables

class pub:
    __z=10 #class private variable
    l=20
    def __init__(self, value):
        self.__privmem=value # private instance variable
        self.pubmem=value # public instance variable
    def __privclass(self):
        self.__privmem+=pub.__z
        print(self.__privmem)
    def pubclass(self):
        print(self.__privmem)
        self.__privclass()
        print(pub.__z)
class subclas(pub):
    def __init__(self,value):
        super().__init__(value)
    def pubmeth(self):
        print(self.__privmem)
x=subclas(20)
print(pub.l)
print(pub.__z)# can not use class private variable

In the above program, we defined a public class variable and private class variables.

The last line in the program is trying to access the private class variable.

We can not access the private class variables. Instead, it will show an error message.