【发布时间】:2021-12-04 07:32:36
【问题描述】:
我有一个继承到三个子类的超类。超类包含一个方法,其输出变量取决于调用该方法的子类。下面,您会看到一个示例,在该示例中,我为方法提供了一个参数来指示要生成的输出。
def dimensionality_reduction(self, algorithm, sparse_weighted_matrix, factors):
ut, _, v = sparsesvd(sparse_weighted_matrix, factors)
if algorithm == 'a':
return ut
elif algorithm = 'b':
return v
else:
raise Exception('Invalid algorithm selected')
相反,我希望这个超类方法能够识别该方法是从哪个子类调用的。
我正在考虑类似的事情:
def dimensionality_reduction(self, sparse_weighted_matrix, factors):
ut, _, v = sparsesvd(sparse_weighted_matrix, factors)
if subclass == 'method_a':
return ut
elif subclass = 'method_b':
return v
else:
raise Exception('Invalid algorithm selected')
有没有办法做到这一点?
【问题讨论】:
-
您是否需要
sparsesvd为其他目的返回这3 个值,或者您可以重写子类方法只是为了返回您在此上下文中需要的值?换句话说,你是在这里调用`sparsesvd方法还是在其他地方调用`sparsesvd方法
标签: python inheritance subclass super