2

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'
Vinay
  • 75
  • 8
  • This question already has answers here: [access-superclass-property-setter-in-subclass](https://stackoverflow.com/questions/42763283) – stovfl Jun 25 '20 at 09:01

1 Answers1

1

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))

Superior
  • 787
  • 3
  • 17
  • Please give a reference to `.fset(...`? – stovfl Jun 25 '20 at 07:59
  • Thank you very much. This is totally new concept for me, Can you please share document link other than python.org if any else give python.org link. Tried searching but couldn't find. – Vinay Jun 25 '20 at 08:11
  • 1
    @Vinay Read up on [fset#property](https://docs.python.org/3/library/functions.html?highlight=fset#property) the last sentence: ***The returned property...*** – stovfl Jun 25 '20 at 08:14
  • @stovfl Thanks, I understand now if I combine this answer and that last line which you refer, but I am still curious how anyone will figure out by just that one line. – Vinay Jun 25 '20 at 08:36
  • @Vinay ***by just that one line.***: If you could, you shouldn't have to ask. Therefore the answer could be improved a lot. Feel free to answer your own question based on this answer to make things clearer. – stovfl Jun 25 '20 at 08:52