@property为什么只能在return私有属性时生效?求大神解答
class Student(object): def __init__(self, name, score): self.name = name self.__score = score @property def score(self): return self.__score @score.setter def score(self, score): if score < 0 or score > 100: raise ValueError('invalid score') self.__score = score @property def grade(self): if self.__score > 80 or self.__score == 80: self.__grade='A' elif self.__score < 60: self.__grade='C' else: self.__grade='B' return self.__grade s = Student('Bob', 59) print s.grade s.score = 60 print s.grade s.score = 99 print s.grade
如果我把s.__grade 改成s.grade 就打印不出了,这是为什呢?