Python 运算符重载

原文地址

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
class Line:
def __init__(self,p1,p2):
self.start = p1
self.end = p2
def __sub__(self,p):
if isinstance(p,Point):
if p is self.start:
return self.end
if p is self.end:
return self.start
class Point:
def __init__(self,x,y):
self.x = x
self.x = x
def __add__(self,p):
if isinstance(p,Point):
return Line(self,p)
if __name__ == '__main__':
p1 = Point(1,2)
p2 = Point(2,3)
#两个点相加 通过__add__方法
line = p1 + p2
print l
#>><__main__.Line instance at 0x02220738>
#线减去一个点 通过__sub__方法
print line - p1
#>><__main__.Point instance at 0x02220710>