Access Modifiers Public and Private in Python || Lesson 43 || Python || Learning Monkey ||

preview_player
Показать описание
#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
Access Modifiers Public and Private in Python
In this class, we discuss Access Modifiers Public and Private in Python.
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.
class pub:
__z=10 #class private variable

def __init__(self, value):
self.__privmem=value # private 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)

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.
The below program is giving an error.
class pub:
__z=10 #class private variable
def __init__(self, value):
self.__privmem=value # private 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)
x.__privmem # private member can not be accessed

Example using Sub Class
class pub:
__z=10 #class private variable
def __init__(self, value):
self.__privmem=value # private 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.__privmem)
x=subclas(20)

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 program display an error message.
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
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.

Link for playlists:

Рекомендации по теме
Комментарии
Автор

how to access the private and protected methods in python

rajyalakshmi
Автор

What if we want to access the private variable and the private function?

SWT-Sathya