-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path007_operator_overloading.py
48 lines (38 loc) · 1.2 KB
/
007_operator_overloading.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
class Person:
def __init__(self, name, surname, age):
self.name = name
self.surname = surname
self.age = age
def __str__(self):
return self.name + " " + self.surname
def __add__(self, other):
return Marriage(self, other)
def __eq__(self, other):
return self.age == other.age
def __neg__(self):
temp = Person(self.name, self.surname, self.age)
temp.age = -1*temp.age
return temp
class Marriage:
def __init__(self, husband, wife):
self.husband = husband
self.wife = wife
def __str__(self):
return self.husband.name + " + " + self.wife.name + " " + self.husband.surname
michal = Person("Michal", "Hucko", 24)
katka = Person("Katka", "Huckova", 60)
print(michal + katka)
print(michal == katka)
print((-michal).age)
print(michal.age) # POZOR objekt sa meni inplace
# __sub__(self, other) -> -
# __mul__(self, other) -> *
# __truediv__(self, other) -> /
# __floordiv__(self, other) -> //
# __mod__(self, other) -> %
# __pow__(self, other) -> **
# __lt__(self, other) -> <
# __gt__(self, other) -> >
# __le__(self, other) -> <=
# __ge__(self, other) -> >=
# __ne__(self, other) -> !=