Class attribute inheritance when using properties in Python
我有以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Desc(object): @property def color_desc(self): return 'Color is ' + self.color @property def brand_desc(self): return 'Brand is ' + self.brand class Text(Desc): def __init__(self): self.color = 'blue' self.brand = 'volvo' def main(): t = Text() print t.color_desc if __name__ == '__main__': main() |
这在运行时正确工作并输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Desc(object): def __init__(self): self._color_desc = color_desc self._brand_desc = brand_desc @property def color_desc(self): return self._color_desc @color_desc.setter def color_desc(self): self._color_desc = 'Color is ' + self.color @property def brand_desc(self): return self._brand_desc @property def brand_desc(self): self._brand_desc = 'Brand is ' + self.brand class Text(Desc): def __init__(self): self.color = 'blue' self.brand = 'volvo' |
突然间,它与
您需要调用父类的
这样做:
1 2 3 4 5 | class Text(Desc): def __init__(self): self.color = 'blue' self.brand = 'volvo' super(Text, self).__init__(0,0) |
desc类的init函数定义不正确。这样做:
1 2 3 4 | class Desc(object): def __init__(self, color_desc, brand_desc): self._color_desc = color_desc self._brand_desc = brand_desc |
在您的
现在还不清楚为什么你认为你的第二个代码片段会起作用。它有以下明显的问题:
Desc.__init__ 使用的两个变量(color_desc 和brand_desc 实际上没有传递给它—您希望它们在全局范围内吗?- 你有两个(不同的!)getters,其中一个返回一些东西,没有为
brand_desc 设置setter。 color_desc setter既不接受也不使用任何参数,那么它到底应该设置什么呢?- 子类
Text.__init__ 不调用或传递任何内容到超类版本,也不执行任务本身。
我认为你的意图是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Desc(object): def __init__(self, color_desc, brand_desc): self._color_desc = color_desc self._brand_desc = brand_desc @property def color_desc(self): return 'Color is ' + self._color_desc @color_desc.setter def color_desc(self, new_color): self._color_desc = new_color @property def brand_desc(self): return 'Brand is ' + self._brand_desc @brand_desc.setter def brand_desc(self, new_brand): self._brand_desc = new_brand class Text(Desc): def __init__(self): super(Text, self).__init__('blue', 'volvo') |
虽然现在看起来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Desc(object): def __init__(self, color, brand): self.color = color self.brand = brand @property def color_desc(self): return 'Color is ' + self.color @property def brand_desc(self): return 'Brand is ' + self.brand car = Desc('blue', 'volvo') car.color = 'red' print car.color_desc # Color is red |
现在直接设置实例的