我有一个高级FTM
class FTM:
def __init__(self,word_weighting = 'normal'):
self.word_weighting = word_weighting
def get_sparse_global_term_weights(self, word_weighting):
pass和继承自FTM的子类。
class FLSA(FTM):
def __init__(self, word_weighting='normal'):
super().__init__(word_weighting = word_weighting)
self.sparse_global_term_weighting = super().get_sparse_global_term_weights(word_weighting = super().word_weighting)运行此代码,将得到以下错误:
AttributeError: 'super' object has no attribute 'word_weighting'我已经初始化了属性。为什么我会有这个错误?
发布于 2022-05-07 15:27:21
超级()只从父类返回一系列绑定方法!
看看这个link:https://realpython.com/python-super/#a-super-deep-dive
通过包含实例化对象,
()返回绑定方法:绑定到对象的方法,该方法给出对象的上下文,例如任何实例属性。如果不包括此参数,则返回的方法只是一个函数,与对象的上下文无关。
不能返回类的属性,您需要为"word_weighting“定义一个属性。
@property
def word_weighting(self):
return self._word_weighting # attrib: 'word_weighting' was changed it to '_word_weighting'https://stackoverflow.com/questions/69636611
复制相似问题