直接使用修改属性不符合面向对象设计中的封装原则,但是使用get,set方法太过麻烦,python提供了@property方法来解决这个问题。
class Student(object):
@property
def score(self):
return self.__score
@score.setter
def score(self,value):
if not isinstance(value,int):
self.__score=value
s=Student()
s.score=60
详细看这篇文章:http://python.jobbole.com/80955/