为什么把def grade(self)上面的@property去掉,s.grade返回的就是这个:<bound method Student.grade of <__main__.Student object at 0x029770D0>>
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: grade = 'A' elif self.__score < 60: grade = 'C' else: grade = 'B' return grade s = Student('Bob', 59) print s.grade s.score = 60 print s.grade s.score = 99 print s.grade
代码如上,我的IDE是PyCharm EDU显示结果是:
<bound method Student.grade of <__main__.Student object at 0x029770D0>> <bound method Student.grade of <__main__.Student object at 0x029770D0>> <bound method Student.grade of <__main__.Student object at 0x029770D0>>