-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtutorial22-access-modifiers.py
91 lines (64 loc) · 1.47 KB
/
tutorial22-access-modifiers.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
"""
Access Modifiers in Python
Public:
Attributes are always public and can be accessed using the dot (.) within the class and outside the class using its object
Protected:
Protected method are created by using prefix _ (single underscore)
Private:
Private members are created by using prefix __ (double underscore) and can not be accessed directly via its object.
"""
"""
class A():
def __init__(self, a):
self.a = a
def display(self):
print('a in class A=', self.a)
class B(A):
def __init__(self, a, b):
A.__init__( self, a)
self.b = b
def display_b(self):
print('a in class B=', self.a)
a1 = A(10)
a1.display()
print(a1.a)
b1 = B(10, 20)
b1.display_b()
print(b1.a)"""
#protected Members
"""
class A():
def __init__(self, a):
self._a = a
def display(self):
print('a in class A=', self._a)
class B(A):
def __init__(self, a, b):
A.__init__( self, a)
self.b = b
def display_b(self):
print('a in class B=', self._a)
a1 = A(10)
a1.display()
print(a1._a)
b1 = B(10, 20)
b1.display_b()
print(b1._a)
"""
#private members
class A():
def __init__(self, a):
self.__a = a
def display(self):
print('a in class A=', self.__a)
class B(A):
def __init__(self, a, b):
A.__init__( self, a)
self.b = b
def display_b(self):
print('a in class B=', self._a)
a1 = A(10)
a1.display()
print(a1._A__a)
b1 = B(100, 200)
print(b1._A__a)