Emi*_*mil 3 inheritance class super attributeerror python-3.x
我有一个超级班FTM:
class FTM:
def __init__(self,word_weighting = 'normal'):
self.word_weighting = word_weighting
def get_sparse_global_term_weights(self, word_weighting):
pass
Run Code Online (Sandbox Code Playgroud)
以及继承自的子类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)
Run Code Online (Sandbox Code Playgroud)
运行此代码,我收到以下错误:
AttributeError: 'super' object has no attribute 'word_weighting'
Run Code Online (Sandbox Code Playgroud)
我已经初始化了该属性。为什么我会收到此错误?
小智 5
super()只返回父类的一系列绑定方法!
\n看看这个link: https: //realpython.com/python-super/#a-super-deep-dive
\n\n通过包含实例化对象,super() 返回一个绑定方法:绑定到该对象的方法,该方法为该方法提供了对象 xe2x80x99s 上下文,例如任何实例属性。如果不包含此参数,则返回的方法只是一个函数,与对象\xe2\x80\x99s 上下文无关。
\n
super()不能返回类的属性,您需要为“word_weighting”定义一个属性。
\n@property\ndef word_weighting(self):\n return self._word_weighting # attrib: \'word_weighting\' was changed it to \'_word_weighting\'\nRun Code Online (Sandbox Code Playgroud)\n