How to set super attribute defined in base class using property decorator?

Viewed 47

I am trying to set base class attribute for a property decorator from the object of derived class
but when I tried to set it throw error.
Here is the code snippets

class stats:
    
    _abcd = 5
    
    @property
    def abcd(self):
        return self._abcd
    
    @abcd.setter
    def abcd(self, value):
        print('base class')
        self._abcd = value

class dele(stats):
    
    @stats.abcd.setter
    def abcd(self, value):
        print('derived class')
        super().abcd = value

a = dele()
a.abcd = 7
print(a.abcd)

and this is the error which I got

derived class
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-9271295df674> in <module>
     20 
     21 a = dele()
---> 22 a.abcd = 7
     23 print(a.abcd)

<ipython-input-23-9271295df674> in abcd(self, value)
     17     def abcd(self, value):
     18         print('derived class')
---> 19         super().abcd = value
     20 
     21 a = dele()

AttributeError: 'super' object has no attribute 'abcd'
1 Answers

Yeah, super in python acts in weird ways

the workaround is not pretty

    @stats.abcd.setter
    def abcd(self, value):
        print('derived class')
        stats.abcd.fset(self, value)

And this cannot either be solved with using super to access superclass (super(stats, dele).p.fset(self, value))

Related