Mar-14-2022, 11:03 PM
I am lacking basic knowledge of Object Oriented Programming.
The situation
Let's assume within a main function I have created two instances. One based on class A, the other one based on class B.
Class A contains data in an attribute. A method of Class B needs access to those data.
|-----class A-----| |-----class B-----|
| Attribut x <--- |---|--Method y |
|-------------------| |-------------------|
How to do this?
Usually, I would hand over the needed values x, when class B is initiated (somehow like call by value): instance of B = B(value x)
The issue: class B will also change and update the value of x and a 3rd class C needs later access to this updated value. Which somehow makes me thinking in the direction of handing over a pointer of value x to class B (which I think you don't do in Python).
You can think of class A as a "data-storage".
(Another thought I had was to use inheritance ... but doesn't look for me the right choice, because this problem is not about common attributes, etc.)
Side note: where I have this problem
Indeed, I am more interested how in general the solution to such a problem should look like - is there a general concept? Nonetheless, I have this problem currently in following situation: with PyQt6 I designed a GUI. This GUI displays data coming from class A (which class B gets through "call by value"). But the GUI also allows the user to change the displayed data. Those changes should now be stored back into class B.
(Remark: the code, especially from the GUI, is very long already, so I did not copy it here to avoid to much noise)
Thanks in advance for all comments, tips, hints, etc.
The situation
Let's assume within a main function I have created two instances. One based on class A, the other one based on class B.
Class A contains data in an attribute. A method of Class B needs access to those data.
|-----class A-----| |-----class B-----|
| Attribut x <--- |---|--Method y |
|-------------------| |-------------------|
How to do this?
Usually, I would hand over the needed values x, when class B is initiated (somehow like call by value): instance of B = B(value x)
The issue: class B will also change and update the value of x and a 3rd class C needs later access to this updated value. Which somehow makes me thinking in the direction of handing over a pointer of value x to class B (which I think you don't do in Python).
You can think of class A as a "data-storage".
(Another thought I had was to use inheritance ... but doesn't look for me the right choice, because this problem is not about common attributes, etc.)
Side note: where I have this problem
Indeed, I am more interested how in general the solution to such a problem should look like - is there a general concept? Nonetheless, I have this problem currently in following situation: with PyQt6 I designed a GUI. This GUI displays data coming from class A (which class B gets through "call by value"). But the GUI also allows the user to change the displayed data. Those changes should now be stored back into class B.
(Remark: the code, especially from the GUI, is very long already, so I did not copy it here to avoid to much noise)
Thanks in advance for all comments, tips, hints, etc.