You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
print(repr(r1)) # This is the case of - when we just write r1 in the interactive console and run it - and it gives the - repr(object).
46
+
47
+
r2=Rectangle(10 ,20)
48
+
print(r1==r2)
49
+
print(r1==100)
50
+
print(r1<r2)
51
+
print(r1>r2) # will give the correct output even though __gt__ method is not defined, as in this case python first sees for __gt__ method when not found will
52
+
# find its negation that is __lt__ method which is implemented and hence will use the ouput from it and negate it and hance provide the correct output.
53
+
# But this will not gonna happen with >== or <== as their complement is not defined.
54
+
r1.width=100
55
+
print(repr(r1))
56
+
57
+
delRectangle
58
+
59
+
60
+
61
+
#---- Using getters and setters ----
62
+
classRectangle:
63
+
def__init__(self, width, height):
64
+
self._width=width# using one underscore is just customary to tell the other user that these are private and not use them directly
65
+
self._height=height# and in-place use the getters and setters method.
# Now for controlling the access for our encapsulated data, we generally uses getters and setters. Suppose for this case we don't need negative value for width as it is not possible.
122
+
123
+
# As we have the getters and setters, so the people should be accessing the width and height using get_width and set_width methods and not directly by obj._width or obj_height.
124
+
# If here, we will try to get obj.width or obj.height it will give us `Attribute Error`, but if we will try to set value obj.width = -100 or obj.height= -100 then it will create a new attribute
125
+
# of these names. This process is called Monkey Patching.
126
+
127
+
# But if we have chagned the attributes to customary private notations, and forces to use getters ans setters then, if the people have written the code as obj.width = 100 or print(obj.width), then
128
+
# the code will break at those points and they have to change their code.
129
+
130
+
r=Rectangle(10, 20)
131
+
print(repr(r))
132
+
# print(r.width) --> will give the `Attribute Error`
133
+
r.width=100# This is Monkey Patching and not setting of width attribute value as the attribute is _width whose value will remain same.
134
+
print(repr(r))
135
+
r.set_width=100
136
+
print(repr(r))
137
+
138
+
delRectangle
139
+
140
+
141
+
142
+
#---- Solution for breaking of code problem ----
143
+
# - Don't force the getters and setters until they are providing some extra functionality like in this case of width and height we have extra functionalty that is, width and height cannot
144
+
# be negative.
145
+
# - Also for those attributes, for whom we need to use setters and getters we make it transparent without letting the user know that a setter is being used, as the user will be directly accessing
146
+
# using name but in background the setter or getter will be runnig.
147
+
classRectangle:
148
+
def__init__(self, width, height):
149
+
self._width=width
150
+
self._height=height
151
+
152
+
defarea(self):
153
+
returnself._width*self._height
154
+
155
+
defperimeter(self):
156
+
return2* (self._width+self._height)
157
+
158
+
# property is use to make the property to be passed through a getter
159
+
@property
160
+
defwidth(self):
161
+
''' This is a getter method which is marked as a property using property decorator'''
162
+
returnself._width
163
+
164
+
@property
165
+
defheight(self):
166
+
''' This is getter method which is marked as property using property decorator '''
167
+
returnself._height
168
+
169
+
# Now since we have the width and height as a property, we can use the proprty.setter to mark that the property
# Since we have made the getters and setters as the property we can directly use them using object without calling them using paranthesis.
204
+
# So, if the name of the getters and setters method are same as that of property without _, then the problem of breaking of code will resolve as
205
+
# we can get or set the width and height using the getters and setters by using just the name of the property.
206
+
207
+
r=Rectangle(10, 20)
208
+
print(r.width)
209
+
r.width=100
210
+
print(repr(r))
211
+
212
+
# Now the above code works fine for most of the things, but there is still a problem that initialisation method can take negative values of width or height.
213
+
# So what can i do is:
214
+
# - Use exception handling
215
+
# or
216
+
# - Use setters in __init__
217
+
218
+
delRectangle
219
+
220
+
221
+
222
+
#---- Using Setters for initialisation ----
223
+
classRectangle:
224
+
def__init__(self, width, height):
225
+
self.width=width# setter is called
226
+
self.height=height# setter is called
227
+
228
+
defarea(self):
229
+
returnself._width*self._height
230
+
231
+
defperimeter(self):
232
+
return2* (self._width+self._height)
233
+
234
+
@property
235
+
defwidth(self):
236
+
''' This is a getter method which is marked as a property using property decorator'''
237
+
returnself._width
238
+
239
+
@property
240
+
defheight(self):
241
+
''' This is getter method which is marked as property using property decorator '''
0 commit comments