-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path41_Encapsulation.py
More file actions
105 lines (63 loc) · 2.17 KB
/
41_Encapsulation.py
File metadata and controls
105 lines (63 loc) · 2.17 KB
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
# Encapsulation :
# prevent accidental modification :
class College :
def __init__(self) :
self.balance = 500000
c = College()
print(c.balance)
c.balance = 100
print(c.balance)
print('-'*20)
# prevention accidental modification :
class College :
def __init__(self) :
self._balance = 500000 # for private variable # data hiding
c = College()
# print(c.balance) #AttributeError: 'College' object has no attribute 'balance'. Did you mean: '_balance'?
# encapsulation :
class College :
def __init__(self) :
self.__balance = 500000 # for private variable # data hiding
def getBalance(self) :
return self.__balance
c = College()
print(c.getBalance())
print('-'*20)
# a)
class College :
def __init__(self) :
self.__balance = 500000 # for private variable # data hiding
def getBalance(self, password) :
if password == 'archana98@' : # for authorized person accessing
return self.__balance
else :
return "Invalid user"
c = College()
# print(c.getBalance()) #TypeError: College.getBalance() missing 1 required positional argument: 'password'
print(c.getBalance('archu123@'))
print('-'*20)
# b)
class College :
def __init__(self) :
self.__balance = 500000 # for private variable # data hiding
def getBalance(self, password) :
if password == 'archana98@' : # for authorized person accessing
return self.__balance
else :
return "Invalid user"
c = College()
print(c.getBalance('archana98@'))
print('-'*20)
# c)
class College :
def __init__(self) :
self.__balance = 500000 # for private variable # data hiding
def getBalance(self, password) :
if password == 'archana98@' or password == 100245 : # for authorized person accessing
return self.__balance
else :
return "Invalid user"
c = College()
print(c.getBalance('archana98@'))
print(c.getBalance(100245)) # for another authorozed person
print(c.getBalance(100243))