-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.py
79 lines (60 loc) · 1.62 KB
/
point.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
class point:
def __init__(self, x, y):
self.x = x
self.y = y
@property
def x(self):
return self.__x
@x.setter
def x(self, x):
self.__x = x
@property
def y(self):
return self.__y
@y.setter
def y(self, y):
self.__y = y
def __add__(self, other):
return point(self.x+other.x, self.y+other.y)
def __iadd__(self, other):
self.x += other.x
self.y += other.y
return self
def __sub__(self, other):
return point(self.x-other.x, self.y-other.y)
def __isub__(self, other):
self.x -= other.x
self.y -= other.y
return self
def __mul__(self, num):
return point(self.x*num, self.y*num)
def __imul__(self, num):
self.x = self.x*num
self.y = self.y*num
return self # 为什么要return self?
def __truediv__(self, num):
return point(self.x/num, self.y/num)
def __itruediv__(self, num):
self.x, self.y = (self.x/num, self.y/num)
return self
def __floordiv__(self, num):
return point(self.x//num, self.y//num)
def __ifloordiv__(self, num):
self.x, self.y = (self.x//num, self.y//num)
return self
def __str__(self):
return 'point({}, {})'.format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
if __name__ == '__main__':
p = point(1, 2)
print(p*2)
p *= 3
print(p)
print(p//2)
print(p/2)
print(__file__)
from pprint import pprint
pprint(globals())
print('\n\n')
pprint(locals())