为什么不能继承Python属性和属性设置器?

时间:2017-11-20 21:06:42

标签: python properties abc

我的班级层次结构如下:

- Foo_ABC      # abstract base class
  - Baz_ABC    # abstract base class
    - Baz1
    - Baz2
  - Bar
  ...

Baz_ABC定义了一个abstractproperty thing,但也实现了setter @thing.setter,因为Baz1Baz2的代码相同。但在我的测试中,我无法设置thing

>>> b = baz1()
>>> b.thing = 42
AttributeError: "can't set attribute" on b.thing

只需将@thing.setter移动到子类Baz1Baz2即可解决此问题。为什么?是的,我知道我可以call super from the subclasses。但我想知道为什么Python要求类定义getter和setter。

0 个答案:

没有答案