-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_lt_eq.py
65 lines (46 loc) · 2.14 KB
/
03_lt_eq.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
55
56
57
58
59
60
"""
Ukázka implementace reprezentace vyplnění dunder metody __repr__()
Funguje to obdobně jako u __repr__()
Dunder metody jsou volány při porovnávání instancí příslušnými operátory
__eq__() ==
__ne__() !=
__gt__() >
__lt__() <
__ge__() >=
__le__() <=
V příkladu jsou některé zakomentované. Je to z důvodu, že ve skutečnosti všechny nejsou třeba.
Python si dokáže odvodit opačnou funkci. Např. z __eq__ si odvodí __ne__
"""
class Person:
def __init__(self, name, surname, age, height):
self.name = name
self.surname = surname
self.age = age
self.height = height
def __repr__(self):
return f"class: {self.__class__.__name__}," \
f" name: {self.name}, surname: {self.surname}, age: {self.age}, height: {self.height}"
def __eq__(self, other):
return (self.name, self.surname, self.age, self.height) == \
(other.name, other.surname, other.age, other.height)
def __lt__(self, other):
return ((self.name, self.surname, self.age, self.height) <
(other.name, other.surname, other.age, other.height))
def __ge__(self, other):
return ((self.name, self.surname, self.age, self.height) >=
(other.name, other.surname, other.age, other.height))
# def __ne__(self, other):
# return (self.name, self.surname, self.age, self.height) != \
# (other.name, other.surname, other.age, other.height)
# def __le__(self, other):
# return ((self.name, self.surname, self.age, self.height) <=
# (other.name, other.surname, other.age, other.height))
# def __gt__(self, other):
# return ((self.name, self.surname, self.age, self.height) >
# (other.name, other.surname, other.age, other.height))
john = Person("John", "Doe", 66, 180)
john_copy = Person("John", "Doe", 66, 180)
great_zohn = Person("Zohn", "Zow", 666, 1800)
print(f"{john}\n{john_copy}\njohn == john_copy: {john == john_copy}\n")
print(f"{john}\n{great_zohn}\njohn == great_zohn: {john == great_zohn}\n")
print(f"{john}\n{great_zohn}\njohn < great_zohn: {john < great_zohn}\n")