-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patharithmetic_operators.py
More file actions
60 lines (51 loc) · 1.41 KB
/
arithmetic_operators.py
File metadata and controls
60 lines (51 loc) · 1.41 KB
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
from math import hypot
class Vector:
"""
A class emulating numeric types.
Some of the special methods usage are demonstrated here.
"""
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
"""
Or you will see sth like <__main__.Vector object at 0x10f673dd8>
The str returned by __repr__ should be unambiguous,if possible,match
the source code necessary to recreate the object being represented.
>>> Vector(3, 4) # test __repr__
Vector(3, 4)
"""
return 'Vector(%r, %r)' % (self.x, self.y)
def __abs__(self):
"""
>>> abs(Vector(3, 4)) # test __abs__
5.0
"""
return hypot(self.x, self.y)
def __bool__(self):
"""
>>> bool(Vector(1, 2)) # test __bool__
True
>>> bool(Vector(0, 0))
False
"""
return bool(self.x or self.y)
def __add__(self, other):
"""
>>> Vector(1, 1) + Vector(1, 2) # test __add__
Vector(2, 3)
"""
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __mul__(self, scalar):
"""
>>> Vector(1, 1) * 3 # test __mul__
Vector(3, 3)
"""
x = scalar * self.x
y = scalar * self.y
return Vector(x, y)
if __name__ == "__main__":
import doctest
doctest.testmod()