My code :
class parent(object):
@property
def value(self):
return self.val
class child(parent):
#__init__
@parent.value.setter
def value(self):
return self.get_val()
This code is not setting the property value to self.get_val() , instead it is using the old value that is set in the parent.
Here , the parent class has no specific setter method but I need to set the property to a different value in the subclass. How do I do it ? Please help.
As mentionned in the comments, the code for your setter is incorrect.
Here is a working solution :
class Parent(object):
def __init__(self, val):
self.val = val
@property
def value(self):
return self.val
class Child(Parent):
@Parent.value.setter
def value(self, val):
self.val = val + " overridden"
parent = Parent("initial")
print(parent.value)
child = Child("initial")
print(child.value)
child.value = "new value"
print(child.value)
which produces
initial
initial
new value overridden