Ahs*_*Zeb 5 python derived-class super
我是python的新手。不知何故
__init__
Run Code Online (Sandbox Code Playgroud)
对于从另一个类派生的类调用两次
super()
Run Code Online (Sandbox Code Playgroud)
我的问题是如何避免这种情况,因为我在那里进行了非常昂贵的计算。
class A(object):
def __new__(cls, *args, **kwargs):
print("Class A: __new__")
obj = super(A, cls).__new__(cls) # super is used here
obj.__init__(*args, **kwargs)
return obj
def __init__(self, x):
self.attrib = x+1
class B(A):
def __init__(self, x):
print("Class B: __init__")
self.prop = 2*x # some expensive computation
a = A(10) # a test call
b = B(20) # Q: here, how to avoid calling __init__ twice in class B?
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢两位的回答。我的真实代码是使用 scipy 库中内置的 arpack 对角化一个大型稀疏矩阵。我正在调用在 arpack.py 中定义的类 SpLuInv(LinearOperator),其中类 LinearOperator 在 interface.py 中定义,两个文件都附加:arpack.py和interface.py。当我调用 SpLuInv() 时,它的init被调用了两次。从你的回答来看,我想我需要删除 obj. init在LinearOperator() 的new中。
感谢 Brendan Abel 的回答以及 Akshat Mahajan 和 Mike Graham 的评论。删除
obj.__init__
Run Code Online (Sandbox Code Playgroud)
来自
__new__
Run Code Online (Sandbox Code Playgroud)
的
LinearOperator()
Run Code Online (Sandbox Code Playgroud)
解决了这个问题。:)
你不应该手动调用__init__在__new__。返回的对象__new__将自动__init__调用。
您应该__init__在所有类中调用超类,即使它们只继承自object.
唯一出现问题的情况是单例对象之类的东西,它们通常__init__从__new__. 在这种情况下,您只需将类的实例存储为类属性,并直接从__init__属性设置时返回。
class A(object):
def __new__(cls, *args, **kwargs):
print("Class A: __new__")
obj = super(A, cls).__new__(cls) # super is used here
return obj
def __init__(self, x):
super(A, self).__init__()
self.attrib = x+1
class B(A):
def __init__(self, x):
print("Class B: __init__")
super(B, self).__init__(x)
self.prop = 2*x # some expensive computation
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1961 次 |
| 最近记录: |