May-08-2020, 10:40 PM
Hi all,
Trying to understand getter/setter...
The following code works fine.
B.x always returns 1 more than A.x (only interested in getters in this example)
100
101
Now, imagine A is slightly changed
--> A has no more getter/setter, and its attribute self._x is now self.x
The following code does not work anymore.
AttributeError: 'super' object has no attribute 'x'
I understand that the interpreter looks for a "x getter" in A, but there is none.
How to implement this, so that I still can use testB.x to get the value +1, WITHOUT CHANGING A.
Obviously I could create a function get_x_plus_1() in B, but it is not the purpose. I want textB.x
Any idea ?
Thanks for your help, much appreciated.
Best regards,
Nico
Trying to understand getter/setter...
The following code works fine.
B.x always returns 1 more than A.x (only interested in getters in this example)
class A(object):
def __init__(self):
self._x = 100
@property
def x(self):
return self._x
@x.setter
def x(self, v):
pass
class B(A):
@property
def x(self):
return super().x + 1
@x.setter
def x(self, v):
pass
testA = A()
testB = B()
print(testA.x)
print(testB.x)Result : 100
101
Now, imagine A is slightly changed
--> A has no more getter/setter, and its attribute self._x is now self.x
The following code does not work anymore.
class A(object):
def __init__(self):
self.x = 100
class B(A):
@property
def x(self):
return super().x + 1
@x.setter
def x(self, v):
pass
testA = A()
testB = B()
print(testA.x)
print(testB.x)Result :AttributeError: 'super' object has no attribute 'x'
I understand that the interpreter looks for a "x getter" in A, but there is none.
How to implement this, so that I still can use testB.x to get the value +1, WITHOUT CHANGING A.
Obviously I could create a function get_x_plus_1() in B, but it is not the purpose. I want textB.x
Any idea ?
Thanks for your help, much appreciated.
Best regards,
Nico
