22# Example : A capsule tablet contains many types of chemicals, therefore it's called as a Capsule.
33
44class SampleClass (object ):
5+
6+ # below two global variables are nothing but a class attributes.
57 globalNumVariable = 3691
68 globalStringVariable = "Some string.."
79
810 b = 987 # b is a global variable
911 _b = 654 # can be called as protected
1012 __b = 321 # private
1113
12- # constrcutor.
14+ # prameterized constrcutor, why do we call as parameterized constructor?
1315 def __init__ (self ):
14- self .a = 123 # OK to access directly
16+ self .a = 123 # OK to access directly only by object.
1517 self ._a = 456 # should be considered protected
1618 self .__a = 789 # considered private, name mangled , more secure Data
1719
@@ -27,9 +29,10 @@ def _getProtectedData(self):
2729 # Below method (which is inside the class) is considered as Private method(used double underscore
2830 # before the method name.
2931 def __getMoreSecureDataMethod (self ):
30- print (self .__a )
32+ print (self .__a ) #directlt printing self.__a private member variable.
3133
3234if __name__ == '__main__' :
35+
3336 # Lets first try to create an object of SampleClass.
3437 objOfSampleClass = SampleClass () # object will get created here.
3538 m = SampleClass () # creating another object for the same class (reason : n number of obj can be created for a class)
@@ -39,7 +42,7 @@ def __getMoreSecureDataMethod(self):
3942 print (objOfSampleClass ._b )
4043 # If you want to bring private value/variable outside the class, normally its not possible.
4144 # Below is going to throw an error (uncomment and execute it)
42- print (objOfSampleClass .__b )
45+ # print(objOfSampleClass.__b)
4346
4447 # But if you do still, want to bring the value outside the class. then follow the below line of code.
4548 # using objOfSampleClass, lets call two global variables
0 commit comments