AttributeError: 'super' object has no attribute 'word_weighting'
AttributeError: 'super' object has no attribute 'word_weighting'
我有一个超级class 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:
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'
我已经初始化了属性。为什么会出现此错误?
super() 只有 return 一系列 绑定方法 来自parent class!
看看这个link
:https://realpython.com/python-super/#a-super-deep-dive
By including an instantiated object, super() returns a bound method: a
method that is bound to the object, which gives the method the
object’s context such as any instance attributes. If this parameter is
not included, the method returned is just a function, unassociated
with an object’s context.
super()不能return一个class的属性,需要定义一个属性为“word_weighting".
@property
def word_weighting(self):
return self._word_weighting # attrib: 'word_weighting' was changed it to '_word_weighting'
我有一个超级class 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:
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'
我已经初始化了属性。为什么会出现此错误?
super() 只有 return 一系列 绑定方法 来自parent class!
看看这个link
:https://realpython.com/python-super/#a-super-deep-dive
By including an instantiated object, super() returns a bound method: a method that is bound to the object, which gives the method the object’s context such as any instance attributes. If this parameter is not included, the method returned is just a function, unassociated with an object’s context.
super()不能return一个class的属性,需要定义一个属性为“word_weighting".
@property
def word_weighting(self):
return self._word_weighting # attrib: 'word_weighting' was changed it to '_word_weighting'