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>