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一樣被寫入字串'-'
請問這是什麼原因