-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessModifiers.py
250 lines (186 loc) · 7.26 KB
/
AccessModifiers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# WHAT IS ACCESS MODIFIER or ACCESS SPECIFIER ?
'''
- Access specifiers or access modifiers in python programming are used to limit the access of class variables and class
methods outside of class while implementing the concepts of inheritance. This can be achieved by: Public, Private
and Protected keyword.
- We can easily inherit the properties or behaviour of any class using the concept of inheritance. But some classes also
holds the data (class variables and class methods) that we don’t want other classes to inherit. So, to prevent that
data we used access specifiers in python.
• NOTE: Python does not follow Access Modifier rule complitely.
'''
# Keywords of Access Modifier
'''
1. Public
2. Protected
3. Private
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# 1. PUBLIC ACCESS MODIFIER:
'''
- only variable
- We can access it in the class, outside the class and also in other class.
- self.name --> Public by default
'''
# ---------------------------------------------------------------------------- #
# 2. PROTECTED ACCESS MODIFIER:
'''
- Single underscore ( _ ) prefixed to the variable
- We can access it in the same class and in immediate child.
- self._name --> Protected
'''
# ---------------------------------------------------------------------------- #
# 3. PRIVATE ACCESS MODIFIER:
'''
- Double underscore ( __ ) prefixed to the variable
- we can access it in self(own) in the same class.
- self.__name --> Private
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# EXAMPLE: 1
'''
class student:
def __init__(self, name, age, std):
self.name = name # Public
self._age = age # Protected
self.__std = std # Private
def show(self):
print(self.name)
print(self._age)
print(self.__std)
obj = student('Nitin', 26, '20th')
obj.show()
'''
# EXAMPLE: 2
'''
class student:
def __init__(self, name, age, std):
self.name = name # PUBLIC
self._age = age # PROTECTED
self.__std = std # PRIVATE
def show(self):
pass
obj = student('Nitin', 26, '10th')
print(obj.name)
print(obj._age)
print(obj.__std) # AttributeError
'''
# EXAMPLE: 3
'''
class student:
def __init__(self, name, age, std):
self.name = name # PUBLIC
self._age = age # PROTECTED
self.__std = std # PRIVATE
def show(self):
print(self.name)
print(self._age)
print(self.__std)
class professor(student):
def __init__(self, name, age, std, height):
super().__init__(name, age, std)
self.height = height
self._height = height
self.__height = height
def show(self):
super(professor, self).show()
print(self.height)
print(self._height)
print(self.__height)
obj = professor('Nitin', 26, '20th', '5 ft 10 in')
print(obj.height)
print(obj._height)
print(obj.__height) # AttributeError
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# NAME MANGLING
'''
- A process in which any given identifier with one trailing underscore and two leading underscores is textually
replaced with the _ClassName__Identifier is known as Name mangling.
'''
# SYNTAX:
'''
obj._classname__variable
'''
# EXAMPLE: 1
'''
class student:
def __init__(self, name, age, std):
self.name = name # Public
self._age = age # Protected
self.__std = std # Private
def show(self):
pass
obj = student('Nitin', 26, '20th')
print(obj.name)
print(obj._age)
print(obj._student__std)
'''
# EXAMPLE: 2
'''
class student:
def __init__(self, name, age, std):
self.name = name # Public
self._age = age # Protected
self.__std = std # Private
def show(self):
pass
class professor(student):
def __init__(self, name, age, std, height):
super().__init__(name, age, std)
self.height = height
self._height = height
self.__height = height
def show(self):
super(professor, self).show()
pass
obj = professor('Nitin', 16, '10th', '4.5 feet')
print(obj.height)
print(obj._height)
print(obj._professor__height)
'''
# ○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
# EXAMPLE:
'''
class Modifiers:
def __init__(self, name):
self._protected_member = name
m = Modifiers("NITIN_P29")
print(m._protected_member)
m._protected_member = "NITIN"
print(m._protected_member)
'''
# EXAMPLE:
"""
class Teacher:
val1 = None
_val2 = None
__val3 = None
def __init__(self, val1, val2, val3):
self.val1 = val1
self._val2 = val2
self.__val3 = val3
def dispPublicMembers(self):
print("This is public member: ", self.val1)
def _dispProtectedMembers(self):
print("This is protected member: ", self._val2)
def __dispPrivateMembers(self):
print("This is private member: ", self.__val3)
def accessPrivateMembers(self):
self.__dispPrivateMembers()
class Child(Teacher):
def __init__(self, val1, val2, val3):
Teacher.__init__(self, val1, val2, val3)
def accessProtectedMembers(self):
self._dispProtectedMembers()
obj1 = Child("Hello", "Simon", 100000)
obj1.dispPublicMembers()
obj1.accessProtectedMembers()
obj1.accessPrivateMembers()
# Explanation:
- After writing the above code in python, the output will appear as “ Hello Simon 100000 ”.
- Here, we have parent class as “Teacher” and derived class as “Child”, and the private members are accessed by making
it public member function “def accessPrivateMembers” and it can access private members of the class.
- Also, we have a “Child” class and it inherits the properties of the parent class and also it can access the protected
member’s function of “Teacher” class which is the parent class.
"""
# ☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻☺☻