Python Forum
Getter/Setter : get parent attribute, but no Getter/Setter in parent
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getter/Setter : get parent attribute, but no Getter/Setter in parent
#1
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)

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
Reply
#2
There is definitely a catch-22 going on. You cannot get A.a from inside B because you did not call A.__init__ so there is no A.a. And if you give B an __init__ that calls A.__init__ you cannot define property "a" because B already has an attribute "a".
class A:
    def __init__(self):
        self.a = 0

class B(A):
    def __init__(self):
        super().__init__()
        
    @property
    def a(self):
        return  1
print(B().a)
Output:
Traceback (most recent call last): File "C:\Users\djhys\Documents\Python\Musings\page1.py", line 14, in <module> print(B().a) File "C:\Users\djhys\Documents\Python\Musings\page1.py", line 7, in __init__ super().__init__() File "C:\Users\djhys\Documents\Python\Musings\page1.py", line 3, in __init__ self.a = 0 AttributeError: can't set attribute
Because this is the same as
class B:
    def __init__(self):
        self.a = 0
        
    @property
    def a(self):
        return  self.a + 1
The obvious answer is to write your own language that allows things like this. Alternately you can write python code that follows the rules
class A:
    def __init__(self):
        self.a = 0

class B(A):
    def __init__(self):
        super().__init__()
        
    @property
    def b(self):
        return  self.a + 1

    @b.setter
    def b(self, value):
        self.a = value
Reply
#3
Thank you for your clear explanation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write code that resides in parent directory franklin97355 0 55 10 hours ago
Last Post: franklin97355
  question about using setter, getter and _ akbarza 4 584 Dec-28-2023, 09:43 PM
Last Post: deanhystad
Question Chain object that have parent child relation.. SpongeB0B 10 970 Dec-12-2023, 01:01 PM
Last Post: Gribouillis
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,594 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  Subclass initialized property used in parent class method. Is it bad coding practice? saavedra29 5 1,685 Feb-07-2022, 07:29 PM
Last Post: saavedra29
  Python class doesn't invoke setter during __init__, not sure if's not supposed to? mtldvl 2 3,245 Dec-30-2021, 04:01 PM
Last Post: mtldvl
  How to access parent object attribute Pavel_47 2 8,563 Nov-19-2021, 09:36 PM
Last Post: deanhystad
Question Stopping a parent function from a nested function? wallgraffiti 1 3,621 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Getting parent variables in nested functions wallgraffiti 1 2,100 Jan-30-2021, 03:53 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020