我想B从A并使用__init__from创建一个子类,A因为它最多与一个属性/属性相同。
以下代码显示了我想做的事情
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>
不评估该属性。
我怎样才能正确地做到这一点,而无需手动复制__init__从A?
回首忆惘然
相关分类