可以把异常直接放入grade。不用__score =score.
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
@property
def grade(self):
try:
if 100 >= self.score > 80:
return 'A'
if 80 <= self.score < 60:
return 'C'
if 0 <= self.score <= 60:
return 'B'
else:
raise ValueError('invalid score')
except Exception as e:
pass
s = Student('Bob', 59)
print s.grade
s.score = 60
print s.grade
s.score = 99
print s.grade
s.score = 1000
print s.score
print s.grade