-
Notifications
You must be signed in to change notification settings - Fork 0
/
overloading.py
193 lines (155 loc) · 4.47 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# how the same built-in operator or function shows different behavior for objects of different classes
1 + 2
'Code' + 'Python'
3 * 2
'Python' * 3
# dunder (double under) methods:
# len() >> __len__()
# str() >> __str__()
# + >> __add__()
# [] >> __getitem__()
# ...
a = 'Code Python'
b = ['Code', 'Python']
len(a)
a.__len__()
b[0]
b.__getitem__(0)
dir(a) # full list of methods and dunders for a string
# By default, most of the built-ins and operators will not work with objects of classes
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
order = Order(['banana', 'apple', 'mango'], 'Emma')
len(order) # error!
# You must override the corresponding special methods in the class definition:
############################ __len__() ##############################
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
def __len__(self):
return len(self.cart) # instead of defining a get_cart_len()
order = Order(['banana', 'apple', 'mango'], 'Mark')
len(order)
order.__len__()
############################ __str__() ##############################
class Vector:
def __init__(self, x_coor, y_coor):
self.x_coor = x_coor
self.y_coor = y_coor
def __str__(self):
return f'{self.x_coor}x:{self.y_coor}y'
vector = Vector(3, 4)
str(vector)
vector.__str__()
############################ __repr__() ##############################
class Vector:
def __init__(self, x_coor, y_coor):
self.x_coor = x_coor
self.y_coor = y_coor
def __repr__(self):
return f'Vector({self.x_coor}, {self.y_coor})'
vector = Vector(3, 4)
repr(vector)
v = eval(repr(vector))
v.x_coor
v.y_coor
############################ __bool__() ##############################
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
def __bool__(self):
return len(self.cart) > 0
order1 = Order(['banana', 'apple', 'mango'], 'Mark')
order2 = Order([], 'Daniel')
bool(order1)
bool(order2)
for order in [order1, order2]:
if order:
print(f"{order.customer}'s order is processing...") # Truthy
else:
print(f"Empty order for customer {order.customer}") # Falsey
######################## + __add__() ##############################
a = 'Code'
a + 'Python'
a
a = a + 'Python' # add requires explicit assignment
a
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
def __add__(self, other):
new_cart = self.cart.copy()
new_cart.append(other)
return Order(new_cart, self.customer)
order = Order(['banana', 'apple'], 'Mark')
(order + 'orange').cart
order.cart # no addition
order.customer
order = order + 'orange' # explicit assignment
order.cart
######################## += __iadd__() ##############################
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
def __iadd__(self, other): # __isub__(), __imul__(), __idiv__()
self.cart.append(other)
return self
order = Order(['banana', 'apple'], 'John')
order += 'mango'
order.cart
######################## [] __getitem__() ##############################
class Order:
def __init__(self, cart, customer):
self.cart = list(cart)
self.customer = customer
def __getitem__(self, key): # key: integer | string | slice obj
return self.cart[key]
order = Order(['banana', 'apple', 'orange', 'mango'], 'Joe')
order[0]
order[-1]
order[2:]
'''
Basic Operators :
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operators :
< __lt__(self, other)
> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Assignment Operators :
-= __isub__(self, other)
+= __iadd__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
//= __ifloordiv__(self, other)
%= __imod__(self, other)
**= __ipow__(self, other)
>>= __irshift__(self, other)
<<= __ilshift__(self, other)
&= __iand__(self, other)
|= __ior__(self, other)
^= __ixor__(self, other)
Unary Operators :
– __neg__(self, other)
+ __pos__(self, other)
~ __invert__(self, other)
'''