Discuss / Python / 这样更清晰

这样更清晰

Topic source

class Student(object):

@property
def birth(self):
    return self.__birth
@birth.setter
def birth(self, value):
    self.__birth = value
@property
def age(self):
    return 2015 - self.birth

变量加上 _是保护变量,__是私有变量,用_的话

@property def age(self): return 2015 - self._birth

@property def age(self): return 2015 - self.birth

效果一样,不过一个直接调用属性,一个是调用属性函数,容易混淆。


  • 1

Reply