Overriding an inherited property setter
我有一个名为
1 2 3 4 5 6 7 8 9 10 11 12 | class Node: @property def importance(self): return self._importance @importance.setter def importance(self, new_importance): if new_importance is not None: new_importance = check_type_and_clean(new_importance, int) assert new_importance >= 1 and new_importance <= 10 self._importance = new_importance |
后来,我有一个继承自
一个定理如何继承
我试着这样做:
1 2 3 4 5 6 | class Theorem(Node): @importance.setter def importance(self, new_importance): self.importance = new_importance # hoping this would use the super() setter assert self.importance >= 3 |
您可以直接通过
1 2 3 4 5 6 | class Theorem(Node): @Node.importance.setter def importance(self, new_importance): # You can change the order of these two lines: assert new_importance >= 3 Node.importance.fset(self, new_importance) |
这将在
这就是一般工作中的属性:调用属性的
您可以通过阅读此答案(以及问题)了解有关属性如何工作的更多信息。
一种方法是使用
1 2 3 4 5 6 7 | class Theorem(Node): def _set_importance(self, new): Node.importance.fset(self, new) assert self.importance >= 3 importance = property(Node.importance.fget, _set_importance) |
据我所知,
根据此错误报告,您可以执行以下操作:
1 2 3 4 5 6 7 | class Theorem(Node): def _set_importance(self, new): super(Theorem, Theorem).importance.fset(self, new) assert self.importance >= 3 importance = property(Node.importance.fget, _set_importance) |
然而,这显然有点尴尬;允许
对于更广泛的问题,这是一个完全不同的解决方案,并且不那么复杂:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | class Node: MIN_IMPORTANCE = 1 MAX_IMPORTANCE = 10 @property def importance(self): return self._importance @importance.setter def importance(self, new_importance): if new_importance is not None: new_importance = check_type_and_clean(new_importance, int) assert (new_importance >= self.MIN_IMPORTANCE and new_importance <= self.MAX_IMPORTANCE) self._importance = new_importance class Theorem(Node): MIN_IMPORTANCE = 3 # and that's all it takes! |
在我看来,这表达了:
The only difference between a
Theorem and aNode , as far asimportance
is concerned, is that aTheorem must have animportance of at least3 .
比覆盖属性setter更清楚。
请注意,