如何解决Python:仅允许设置具有@property装饰器的属性
class MyClass():
def __init__(self):
self.attribute_1 = "foo"
self.attribute_2 = "bar"
@property
def attribute_1(self):
return self._attribute_1
@attribute_1.setter
def attribute_1(self,s):
self._attribute_1 = s
@property
def attribute_2(self):
return self._attribute_2
@attribute_2.setter
def attribute_2(self,s):
self._attribute_2 = s
>>> ob = MyClass()
>>> ob.attribute_1 = 'fizz' #Ok
>>> ob.atribute_1 = 'buzz' #want to throw an exception because this has no setter or @property def
如果我们尝试设置未用property和setter装饰的属性,我希望我的班级抱怨。我尝试使用slots,但无法与属性装饰器一起使用。 'attribute' in __slots__ conflicts with class variable
有什么想法吗?
解决方法
__slots__
应该包含所有实例变量,在您的情况下,它们是_attribute_1
和_attribute_2
(内部使用下划线的变量),因此只需执行以下操作:
class MyClass():
__slots__ = ["_attribute_1","_attribute_2"]
pass # rest of implementation
请注意,如果您的属性只是直接转发,则最好将公共变量放在插槽中,并且仅具有需要更多验证或其他逻辑的字段的属性。实际上,拥有插槽实际上是一种财产:
>>> MyClass._attribute_1
<member '_attribute_1' of 'MyClass' objects>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。