ACCESS MODIFIERS: ================= ==> USED TO DEFINE THE SCOPE OF THE DATA. IN JAVA: keywords: public private protected access modifiers In Python: no keywords are available for access modifiers ==> to define the access modifiers, the symbol of notation we can use. 1) public data ==> normal data/variable representation ex: a = 100 self.b = 200 ==> the public data can able to access in anywhere of the program. 2) private data: ==> can define with '__' variable name must be prefix with '__' Syntax: __variable-name = value ==> we can access within the class, but not in outside of the class. 3) protected data: ==> can prefix with '_' Syntax: _variable-name = value # Access modifiers from mmap import ACCESS_READ class AceessModifiers: # Public data a = 100 b = "Python" # private data __c = 12.12 __d = True # protected data _e = 1122 _f = 0b11001 am = AceessModifiers() print("The Public data = ") print(am.a) print(am.b) # We can extend the public data to outside the class. # print("The Private data = ") # print(am.__c) Attribute Error # print(AceessModifiers.__c) Attribute Error # We can't extend the private to outside the class. print("The Protected data = ") print(am._e) print(am._f) # We can extend the protected data to outside the class. print(AceessModifiers._e) ===================================== class AccessModifiers: a = 123 __b = 121 _c = 1122 def printData(self): print("The Public Data is = ",self.a) print("The Private Data is = ",self.__b) print("The Protected Data is = ",self._c) # public, private and protected data can extend to any method within the class. am = AccessModifiers() am.printData() ============================================== class AccessModifiers: a = 123 __b = 121 _c = 1122 def printData(self): print("The Public Data is = ",self.a) print("The Private Data is = ",self.__b) print("The Protected Data is = ",self._c) # public, private and protected data can extend to any method within the class. def publicData(self): self.p = "Python" self.q = True def privateData(self): self.__x = 1234 def protectData(self): self._y = 1221 def checkData(self): print("Public data = ",self.p,self.q) print("The Private Data = ",self.__x) print("The Protected data = ",self._y) def methodParameters(self,s,t,u): return s+t+u @staticmethod def static(): AccessModifiers.__d2 = 1122 # _d2 = 1221 # AccessModifiers.d2 = 1221 def instance(self): print("The Static method data = ",AccessModifiers.__d2) am = AccessModifiers() am.printData() am.publicData() am.privateData() am.protectData() am.checkData() d1 = 121 __d2 = 122 _d3 = 123 print(am.methodParameters(d1,__d2,_d3)) AccessModifiers.static() am.instance() ================================================= Encapsulation: ============== Wrapping/bundling of data and methods into single unit (class) is called as "Encapsulation". program: part-1: code and implementation private data private method-1 private method-2 protected method-3 part-2: code & implementation data methods part-3: code & implementation data methods part-4: code & implementation data methods part-5: code & implementation data methods method-2 Advantages: =========== 1) Code Reusability 2) Security Note: ===== Encapsulation is not achieved with public data & protected data Encapsulation is achieved with private data. class publicData: def __init__(self,name,age): self.name = name self.age = age def printMethod(self): # public method print("Name = ",self.name) print("Age = ",self.age) def __privateMethod(self): print("Name = ",self.name) print("Age = ",self.age) def _protectedMethod(self): self.__privateMethod() obj = publicData("Hyma", 25) obj.printMethod() # obj.__privateMethod() obj._protectedMethod()