|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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):
print(other.a)
print(other.a.__class__)
return Vector(self.a + other.a, self.b + other.b)
v1 = Vector(2,10)
v2 = Vector(5,-2)
print(v1 + v2)
问题:
从网上发现这么一段代码,突然发现了other.a的用法。然后就有点晕,不知道为什么还有这种用法。
尝试对other.a进行打印,已经类型打印,打印结果如下:
5
<class 'int'>
Vector (7, 8)
发下这个other.a实际上应该是加数的赋值,但是这个原理是是什么,没见过这种用法昂,求大神告知
|
|