【发布时间】:2019-03-02 09:10:05
【问题描述】:
我想从A 创建一个子类B 并使用A 中的__init__,因为它最多有一个属性/属性相同。
以下代码显示了我想做的事情
class A:
def __init__(self):
self.a = 1
self.b = 1
self.c = 1
class B(A):
def __init__(self):
super().__init__() # because I want 'a' and 'b', (but not 'c')
@property
def c(self):
return 2
B()
追溯:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-95c544214e48> in <module>()
13 return 2
14
---> 15 B()
<ipython-input-9-95c544214e48> in __init__(self)
7 class B(A):
8 def __init__(self):
----> 9 super().__init__() # because I want 'a' and 'b', (but not 'c')
10
11 @property
<ipython-input-9-95c544214e48> in __init__(self)
3 self.a = 1
4 self.b = 1
----> 5 self.c = 1
6
7 class B(A):
AttributeError: can't set attribute
我认为我可以通过这样做来解决这个问题
class B(A):
def __init__(self):
super().__init__() # because I want 'a' and 'b', (but not 'c')
self.c = property(lambda s: 2)
但是当调用时:
>>> B().c
<property at 0x116f5d7c8>
不评估该属性。
如何在不从A 手动复制__init__ 的情况下正确执行此操作?
【问题讨论】:
-
在我看来,
B根本不应该真正继承自A。一个通用的超类可能是合适的。 -
这里有点难以理解你的用例,因为你已经匿名了太多。你真的需要 B.c 成为一个财产,还是那是一个例证?调用 super() 后不能简单地设置
self.c = 2有什么原因吗? -
用例是(仍然比我想要的更简单)
A的许多方法使用c,但是B添加了一些功能并使c成为@ 987654339@ 而不是number,则属性c取该list的平均值。
标签: python class properties attributes