-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkt_11_oops_02_access_modifiers.py
More file actions
161 lines (120 loc) · 4 KB
/
kt_11_oops_02_access_modifiers.py
File metadata and controls
161 lines (120 loc) · 4 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
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
# 1. Python - Access Modifiers
"""
Public members − A class member is said to be public if it can be accessed from anywhere in the program.
Protected members − They are accessible from within the class as well as by classes derived from that class.
* To imply that a certain instance variable is protected, prefix it with single underscore (such as "_salary").
Private members − They can be accessed from within the class only.
* To indicate that an instance variable is private, prefix it with double underscore (such as "__age").
"""
class Employee:
def __init__(self, name, age, salary):
self.name = name # public variable
self._salary = salary # protected variable
self.__age = age # private variable
def displayEmployee(self):
print("I am from parent==> Name : ", self.name, ", age: ", self.__age, ", salary: ", self._salary)
# e1 = Employee("Bhavana", 24, 10000)
# e1.displayEmployee()
# print(e1.name)
# print(e1._salary)
# # print(e1.__age) # AttributeError: 'Employee' object has no attribute '__age'
class Child(Employee):
def __init__(self, name, age, salary):
self.name = name # public variable
self._salary = salary # protected variable
self.__age = age # private variable
super().__init__(name, age, salary)
def displayEmployee(self):
print("I am from child==> Name : ", self.name, ", age: ", self.__age, ", salary: ", self._salary)
cls_obj = Child('A', 25, '10k')
cls_obj.displayEmployee()
print(cls_obj.name)
print(cls_obj._salary)
# print(cls_obj.__age) # AttributeError: 'Child' object has no attribute '__age'
# 2. Python - Name Mangling
"""
obj._class__privatevar
print (e1._Employee__age)
"""
# 3. Python - Property Object
"""
property(fget=None, fset=None, fdel=None, doc=None)
"""
# 3.1 Getters and Setter Methods without property
class Employee:
def __init__(self, name, age):
self.__name = name
self.__age = age
self.__abc = '1'
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def get_abc(self):
print("called get_abc")
return self.__abc
def set_name(self, name):
self.__name = name
return
def set_age(self, age):
self.__age = age
def set_var(self, abc):
self.__abc = abc
e1 = Employee("Bhavana", 24)
print("Name:", e1.get_name(), "age:", e1.get_age())
e1.set_name("Archana")
e1.set_age(21)
print("Name:", e1.get_name(), "age:", e1.get_age())
e1.set_var('500')
print("Name:", e1.get_name(), "age:", e1.get_age(), 'abc: ', e1.get_abc())
# 3.2 Using Property
class Employee:
def __init__(self, name, age):
self.__name = name
self.__age = age
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def set_name(self, name):
self.__name = name
return
def set_age(self, age):
self.__age = age
return
name = property(get_name, set_name, "name")
age = property(get_age, set_age, "age")
e1 = Employee("Bhavana", 24)
print("Name:", e1.name, "age:", e1.age)
e1.name = "Archana"
e1.age = 23
print("Name:", e1.name, "age:", e1.age)
# 3.3 Using @property decorator
class Portal:
# Defining __init__ method
def __init__(self):
self.__name = ''
# Using @property decorator, @property is by default Getter method so no need to mention that name.getter
@property
# Getter method
def name(self):
return self.__name
# Setter method
@name.setter
def name(self, val):
self.__name = val
# Deleter method
@name.deleter
def name(self):
del self.__name
# Creating object
p = Portal()
# Setting name
p.name = 'Sriram'
# Prints name
print(p.name)
# Deletes name
del p.name
# As name is deleted above this
# will throw an error
# print(p.name)