Skip to content

Instantly share code, notes, and snippets.

@arjunprakash027
Created October 11, 2023 11:28
Show Gist options
  • Save arjunprakash027/066ef38673a8408e95697173f6bbe5e3 to your computer and use it in GitHub Desktop.
Save arjunprakash027/066ef38673a8408e95697173f6bbe5e3 to your computer and use it in GitHub Desktop.
A small example code for access modifiers in python
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