Created
October 11, 2023 11:28
-
-
Save arjunprakash027/066ef38673a8408e95697173f6bbe5e3 to your computer and use it in GitHub Desktop.
A small example code for access modifiers in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class all_modify: | |
#declaring public,proteced and private | |
var1,_var2,__var3 = None,None,None | |
def __init__(self,var1,var2,var3): | |
self.var1 = var1 | |
self._var2 = var2 | |
self.__var3 = var3 | |
def disPub(self): #public method | |
print("public",self.var1) | |
def _disProc(self): #protected mthd- accessed from the derived class | |
print("procted",self._var2) | |
def __disPri(self): #private mthd- only be accessed from inside the class | |
print("private",self.__var3) | |
def DisPriMthd(self): | |
self.__disPri() | |
class sub(all_modify): | |
def __init__(self,var1,var2,var3): | |
all_modify.__init__(self,var1,var2,var3) | |
def DisProcMthd(self): | |
self._disProc() | |
obj = sub(1,2,3) | |
obj.disPub() | |
obj.DisPriMthd() | |
obj.DisProcMthd() | |
print(obj.var1) | |
print(obj._var2) | |
print(obj.__var3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment