#๐Ÿ”’ The best way to implement that?

26 messages ยท Page 1 of 1 (latest)

snow marlin
#

i want to know how to overwrite a property`s setter in a subclass.
the best way i can find is in the image.
but i want to use something like

class nonnegative_int_value(int_value):
    @super(__class__,__class__).data.setter
    def data(self,x):
            x=int(x)
            self._data=x if x>0 else 0

and it failed.....

lethal orchidBOT
#

@snow marlin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

snow marlin
#

why can not i write code that uses super(__class__,__class__) directly inside the class?

#

is that because the __class__ has not existed yet by that moment?

copper aurora
#

probably because the __class__ attribute doesn't exist when the class is defined

#

yeah

#

it's like trying to reference the class name in the class

snow marlin
#

?

copper aurora
#

hm

#

can you send your code as text

#

so I can play around with it

snow marlin
#
class int_value():
    _data=0
    @property
    def data(self):
        return self._data
    @data.setter
    def data(self,x):
        self._data=int(x)

        
class nonnegative_int_value(int_value):
    pass

>>> def data(self,x):
...         x=int(x)
...         self._data=x if x>0 else 0
... 
...         
>>> nonnegative_int_value.data=super(nonnegative_int_value,nonnegative_int_value).data.setter(data)
>>> 
>>> n=nonnegative_int_value()
>>> n.data=-3.3
>>> n.data
0
>>> n.data=4.5
>>> n.data
4
>>> class nonnegative_int_value(int_value):
...     @super(__class__,__class__).data.setter
...     def data(self,x):
...             x=int(x)
...             self._data=x if x>0 else 0
... 
...             
Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    class nonnegative_int_value(int_value):
  File "<pyshell#68>", line 2, in nonnegative_int_value
    @super(__class__,__class__).data.setter
NameError: name '__class__' is not defined


#

use some decorator to runs codes like nonnegative_int_value.data=super(nonnegative_int_value,nonnegative_int_value).data.setter(data) for me?๐Ÿค”

#

or metaclass?

#

๐Ÿค”

snow marlin
#

is there a better solution?

copper aurora
#

not that I've found

snow marlin
copper aurora
#

lol, possibly something to do with metaclasses

snow marlin
#

.......

elder flower
#

you could make the setter call a method, which you then override in your subclass

#

anyways, can simply do without trying to use super

class Parent:
    @property
    def x(self):
        return 0
    
    @x.setter
    def x(self, val):
        print("Parent", val)

class Child(Parent):
    @Parent.x.setter
    def x(self, val):
        # super() if you want in here now possibly, although idk how to make that work
        print("Child", val)
lethal orchidBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.