-
Notifications
You must be signed in to change notification settings - Fork 4
/
lsp.py
54 lines (39 loc) · 1.1 KB
/
lsp.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
class Rectangle:
def __init__(self, width, height):
self._height = height
self._width = width
@property
def area(self):
return self._width * self._height
def __str__(self):
return f'Width: {self.width}, height: {self.height}'
@property
def width(self):
return self._width
@width.setter
def width(self, value):
self._width = value
@property
def height(self):
return self._height
@height.setter
def height(self, value):
self._height = value
class Square(Rectangle):
def __init__(self, size):
Rectangle.__init__(self, size, size)
@Rectangle.width.setter
def width(self, value):
_width = _height = value
@Rectangle.height.setter
def height(self, value):
_width = _height = value
def use_it(rc):
w = rc.width
rc.height = 10 # unpleasant side effect
expected = int(w * 10)
print(f'Expected an area of {expected}, got {rc.area}')
rc = Rectangle(2, 3)
use_it(rc)
sq = Square(5)
use_it(sq)