-
Notifications
You must be signed in to change notification settings - Fork 0
/
Access Specifiers.py
40 lines (35 loc) · 1.02 KB
/
Access Specifiers.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
"""
Python supports 3 access specifiers by using which we can implement emcapsulation and restrict the access of
data members from unauthorized use.
1. public ---> by default every data member is public by nature
2. protected data members---> can be aceesed by the class which defines it and its child classes
3. private ---> These variables can only be used by the class which defines them.
protected ---->
Syntax: _varName
private
Syntax: __varName
"""
class A:
def __init__(self):
self.datapublic = 45
self._dataProtected = 60
self.__dataPrivate = 50
def change(self):
self.datapublic = 10
self._dataProtected = 20
self.__dataPrivate = 30
class B(A):
def modify(self):
self.datapublic = 10
self._dataProtected = 20
self.__dataPrivate = 30
class C:
def m1(self):
print(self._dataProtected)
obj = B()
obj.modify()
print(obj.datapublic)
print(obj._dataProtected)
#print(obj.__dataPrivate)
objC = C()
objC.m1()