-
Notifications
You must be signed in to change notification settings - Fork 0
/
aula147.py
42 lines (37 loc) · 1.19 KB
/
aula147.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
# Exemplo de uso de dunder methods (métodos mágicos)
# __lt__(self,other) - self < other
# __le__(self,other) - self <= other
# __gt__(self,other) - self > other
# __ge__(self,other) - self >= other
# __eq__(self,other) - self == other
# __ne__(self,other) - self != other
# __add__(self,other) - self + other
# __sub__(self,other) - self - other
# __mul__(self,other) - self * other
# __truediv__(self,other) - self / other
# __neg__(self) - -self
# __str__(self) - str
# __repr__(self) - str
class Ponto:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
class_name = type(self).__name__
return f"{class_name}(x={self.x!r}, y={self.y!r})"
def __add__(self, other):
novo_x = self.x + other.x
novo_y = self.y + other.y
return Ponto(novo_x, novo_y)
def __gt__(self, other):
resultado_self = self.x + self.y
resultado_other = other.x + other.y
return resultado_self > resultado_other
if __name__ == "__main__":
p1 = Ponto(4, 2) # 6
p2 = Ponto(6, 4) # 10
p3 = p1 + p2
print(p3)
print("P1 é maior que p2", p1 > p2)
print("P2 é maior que p1", p2 > p1)
print("p3 =", p3)