Discuss / Python / 原来这样写也是可以的

原来这样写也是可以的

Topic source

尝试着这样写了一下:

@property
def get_score(self):
    return self.__score

@get_score.setter    
def set_score(self, score):
    if not isinstance(score, int):
        raise ValueError('score must be an integer')
    if score < 0 or score > 100:
        raise ValueError('score must be between 0 and 100')
    self.__score = score      

然后这样访问:

>>>s.set_score = 70
>>>s.get_score
70

看来,是可以像使用变量一样使用函数了


  • 1

Reply