Skip to content

Commit

Permalink
Classwork 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ykirnev committed Nov 5, 2024
1 parent 4b7d2ef commit 58379ff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions 20241105/0/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Rectangle:
def __init__(self, x1, x2, y1, y2):
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
def __str__(self):
return f'{self.x1, self.y1}{self.x1, self.y2}{self.x2, self.y1}{self.x2, self.y2}'
a = Rectangle(1, 2, 3, 4)
print(a)

41 changes: 41 additions & 0 deletions 20241105/0/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Rectangle:
rectcnt = 0
def __init__(self, x1, y1, x2, y2):
Rectangle.rectcnt += 1
self.__setattr__("rect_" + str(Rectangle.rectcnt), 0)
self.x1 = x1
self.x2 = x2
self.y1 = y1
self.y2 = y2
def __str__(self):
return f'{self.x1, self.y1}{self.x1, self.y2}{self.x2, self.y1}{self.x2, self.y2}'
def square(self):
return abs(self.x1 - self.x2) * abs(self.y1 - self.y2)
def __lt__(self, other):
return self.square() < other.square()
def __eq__(self, other):
return self.square() == other.square()
def __mul__(self, n):
return Rectangle(self.x1 * n, self.y1 * n, self.x2 * n, self.y2 * n)
def __rmul__(self, n):
return Rectangle(n * self.x1, n * self.y1, n * self.x2, n * self.y2)
def __getitem__(self, id):
mas = [(self.x1, self.y1), (self.x1, self.y2), (self.x2, self.y1), (self.x2, self.y2)]
return mas[id]
def __bool__(self):
return self.square() != 0
def __del__(self):
self.rectcnt -= 1
print(f"rectcnt; {Rectangle.rectcnt}")

a = Rectangle(1, 2, 3, 4)
b = Rectangle(2, 2, 3, 4)
print(a == b)
c = Rectangle(1, 2, 3, 4)
d = Rectangle(1, 2, 3, 2)
print(a)
print(Rectangle.rectcnt)
print(a.__dict__)
print(a * 3, 5 * a)
print(a[2], c[-2])
print(a.__bool__(), d.__bool__())

0 comments on commit 58379ff

Please sign in to comment.