2

I have a python class A and its child B. I make an object of class B passing some list-type arguments, which are appended in B and the result stored in class A. I would like to be able to change one of those initial arguments and see the change reflected without further ado. Let me explain with an example:

class A():
    def __init__(self,data):
        self.a=data

class B(A):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        A.__init__(self,self.x+self.y)

    def change_x(self,x):
        self.x = x

Now, wen i run

test = B([1, 2], [3,4])
print(test.a)

I obviously get [1,2,3,4]. If I change part of the list as follows:

test.change_x([3,4])
print(test.a)

I would like to get [3,4,3,4], instead of course, I receive [1,2,3,4]. Of course test.a is only evaluated during instantiation.

I understand why this is not the case and I read about generators but don't manage to figure out how to implement this. I don't want to end up with an iterable that i can only iterate once.

Could anyone help me? A clean way to solve this?

4
  • I could of course try something like: def change_x(self,x): self.__init__(x,self.y) But I'm not sure whether this is a good approach? Commented Aug 21, 2016 at 21:25
  • You could make a a property and override it in class B Commented Aug 21, 2016 at 21:25
  • There isn't really a way to do that with your current design. In your example, why is A a class at all (and why is it a superclass of B)? What you could do is give B a method that does the equivalent of self.a.data, but computes the result dynamically. Basically, when you create a, you are creating it with some data, and a knows nothing about where that data came from. If you want to get updated data, it may be easier to do it in B and not involve a at all. Commented Aug 21, 2016 at 21:27
  • Hi, I tried to make kind of a minimal working design, which lost all meaning. I'm implementing kind of a network protocol, class A is just the container for a packet. class B would have knowledge of header, cargo, trailer split. class, e.g., wifi, would be derrived from B and describe the actual header, and trailer. class A would have a transmit function, class B would just concatenate data and perhaps calculate a crc. Commented Aug 22, 2016 at 6:00

3 Answers 3

6

Is there a reason to have the class A at all? You could create a property or method a that returns what you want within class B.

class B():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def change_x(self, x):
        self.x = x

    @property
    def a(self):
        return self.x + self.y


test = B([1, 2], [3, 4])
print(test.a)  # [1, 2, 3, 4]

test.change_x([3, 4])
print(test.a)  # [3, 4, 3, 4]

Small note: With this implementation, the change_x method shouldn't be necessary. It's generally more Pythonic to just access attributes directly (e.g. test.x = x) than to use getters and setters.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Karin, thank you for your reply. As I just commented above: Hi, I tried to make kind of a minimal working design, which lost all meaning. I'm implementing kind of a network protocol, class A is just the container for a packet. class B would have knowledge of header, cargo, trailer split. class, e.g., wifi, would be derrived from B and describe the actual header, and trailer. class A would have a transmit function, class B would just concatenate data and perhaps calculate a crc change_x could e.g. rather be def mess_up_crc(self) (for testing purposes)
1

You need to implement it "manually". For example, like so:

class B(A):
    def __init__(self,x,y):
        self.x=x
        self.y=y
        self.spread_changes()

    def change_x(self,x):
        self.x = x
        self.spread_changes()

    def spread_changes(self):
        A.__init__(self,self.x+self.y)

Comments

0

I ended up storing all parts of self.a (in A) directly by slicing/indexing from it's inheriting classes. I added a method "B.update_crc(self)" as suggested by Israel Unterman to reevaluate certain parts of self.a in case of a modification.

Thanks, my first approach just wan't the right one :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.