1 回答
TA贡献1864条经验 获得超6个赞
class Spam:
def __init__(self, is_less_sodium):
self.__is_less_sodium = is_less_sodium
Spam.is_less_sodium = property(Spam.__getter,)
def __getter(self):
return self.__is_less_sodium
my_spam = Spam(False)
print(my_spam.is_less_sodium)
您可以使用该语法,但您还必须从类级别引用该函数,而不是实例函数。
装饰器语法更简洁。
class Spam:
def __init__(self, is_less_sodium):
self.__is_less_sodium = is_less_sodium
@property
def is_less_sodium(self):
return self.__is_less_sodium
@is_less_sodium.setter
def set_is_less_sodium(self):
self.__is_less_sodium
my_spam = Spam(False)
print(my_spam.is_less_sodium)
my_spam.is_less_sodium = True
print(my_spam.is_less_sodium)
添加回答
举报