2012-12-18 iteye Python 运算符重载 原文地址 1234567891011121314151617181920212223242526272829303132class 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.startclass 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> 下一篇 OS Mountain Lion 10.8.2 安装 Cython 上一篇 Chrome 扩展