为什么这样print(cat3+cats) 结果是None,但把 return other.append(self) 改成other.append(self)
return other 就可以正常返回,这两个有什么区别吗?
class Cat:
"""
这是一只猫的类
"""
def init(self,name,tail_length=10):
self.taillen=tail_length
self.name=name
print('我是一只猫,我叫 %s' % self.name)
def __add__(self, other):
if isinstance(other,Cat):
return [self,other]
elif isinstance(other,list):
return other.append(self)
cat1=Cat('crystal')
cat2=Cat('Sniper',15)
print(cat1+cat2)
cats=cat1+cat2
cat3=Cat('Alien')
print(cat3+cats)
def __add__(self, other):
if isinstance(other, Cat):
return [self, other]
if isinstance(other, list):
return other + [self]
return 你的return 个值, append都没有返回值,你咋return
append是没有返回值的方法,但调用到它列表就会增加一项