2
2
# Example : A capsule tablet contains many types of chemicals, therefore it's called as a Capsule.
3
3
4
4
class SampleClass (object ):
5
+
6
+ # below two global variables are nothing but a class attributes.
5
7
globalNumVariable = 3691
6
8
globalStringVariable = "Some string.."
7
9
8
10
b = 987 # b is a global variable
9
11
_b = 654 # can be called as protected
10
12
__b = 321 # private
11
13
12
- # constrcutor.
14
+ # prameterized constrcutor, why do we call as parameterized constructor?
13
15
def __init__ (self ):
14
- self .a = 123 # OK to access directly
16
+ self .a = 123 # OK to access directly only by object.
15
17
self ._a = 456 # should be considered protected
16
18
self .__a = 789 # considered private, name mangled , more secure Data
17
19
@@ -27,9 +29,10 @@ def _getProtectedData(self):
27
29
# Below method (which is inside the class) is considered as Private method(used double underscore
28
30
# before the method name.
29
31
def __getMoreSecureDataMethod (self ):
30
- print (self .__a )
32
+ print (self .__a )#directlt printing self.__a private member variable.
31
33
32
34
if __name__ == '__main__' :
35
+
33
36
# Lets first try to create an object of SampleClass.
34
37
objOfSampleClass = SampleClass () # object will get created here.
35
38
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):
39
42
print (objOfSampleClass ._b )
40
43
# If you want to bring private value/variable outside the class, normally its not possible.
41
44
# Below is going to throw an error (uncomment and execute it)
42
- print (objOfSampleClass .__b )
45
+ # print(objOfSampleClass.__b)
43
46
44
47
# But if you do still, want to bring the value outside the class. then follow the below line of code.
45
48
# using objOfSampleClass, lets call two global variables
0 commit comments