【python】property

mac2024-04-04  35

@property 广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性。 简单的说:就是把方法变成属性,既具有属性的方便,且具有方法的功效。

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
最新回复(0)