'AttributeError: 'super' object has no attribute 'word_weighting'

I have a super 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

And a subclass that inherits from 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)
        

Running this code, I get the following error:

AttributeError: 'super' object has no attribute 'word_weighting'

I have initialized the attribute. Why do I get this error?



Solution 1:[1]

super() only returns a series of bound methods from the parent class!

Look at this 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() can not return the attributes of a class, and you need to define a property for the "word_weighting".

@property
def word_weighting(self):
    return self._word_weighting # attrib: 'word_weighting' was changed it to '_word_weighting'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mehrdad-mixtape