标签:python vector return __add__ __str__
#!/usr/bin/python class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return ‘Vector (%d, %d)‘ % (self.a, self.b) def __add__(self,other): return Vector(self.a + other.a, self.b + other.b) v1 = Vector(2,10) v2 = Vector(5,-2) v3 = Vector(10,10) print v1 + v2 + v3 print v1
结果: Vector (17, 18) Vector (2, 10)
本文出自 “知识在于积累” 博客,请务必保留此出处http://goooood.blog.51cto.com/5060201/1580673
标签:python vector return __add__ __str__
原文地址:http://goooood.blog.51cto.com/5060201/1580673