class Teacher(object): def __init__(self, name,score): self.name = name self.__score = score w=Teacher('ab',22) print w._Teacher__score #对于私有属性这样为什么就可以访问啊?
还有一个问题
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 score>=90: return 'A' elif score>=60: return 'B' return 'C' s = Student('Bob', 59) s.__score(1000) print s.__score#这里可以正常打印1000,为什么? s.score=1000 print s.score#这里就会报错?又是为什么?
asd8532
相关分类