class Player():
def __init__(self):
self._age = 0
self._stats = {'PTS': 0, 'SCORE': 0}
@property
def age(self):
return self._age
@age.setter
def age(self, age):
if isinstance(age, int):
self._age = age
else:
self._age = 0
@property
def stats(self):
return self._stats
@stats.setter
def stats(self, val):
if isinstance(val, int):
self._stats = val
else:
self._stats = 0
p1 = Player()
p1.age = '-'
p1.stats['PTS'] = '-'
p1.stats['SCORE'] = 200
print(p1.age, p1.stats['PTS'], p1.stats['SCORE'])
输出结果是
0 - 200
请问在age和stats的setter里一样做了型别的判断
结果只有对age有用
stats一样被写入字串'-'
请问这是什么原因