Python Forum
[Solved] Novice question to OOP: can a method of class A access attributes of class B
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Novice question to OOP: can a method of class A access attributes of class B
#1
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.
Reply
#2
It is not a good idea to have a class reference attributes of an unrelated (not a superclass) class. It is almost acceptable if class B is responsible for creating creating instances of class A. "I brought you into this world and I can can check your credit card balance." It is still bad parenting, but sometimes required. If there are A objects outside the scope of B, and B does things other than cater to A, it is a better design to have a helper function that knows about A and B and implements the passage of information between them.

As to the PyQt example, I would either bind the control's signal to a method in A, or have the control call a helper function that knows about A.
BigMan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python class members based on a type value voidtrance 6 601 Apr-07-2025, 06:56 PM
Last Post: buran
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,229 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  printing/out put issue with class arabuamir 3 894 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 3,505 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  sharepoint: Access has been blocked by Conditional Access policies CAD79 0 2,071 Jul-12-2024, 09:36 AM
Last Post: CAD79
  [split] Class and methods ebn852_pan 15 3,135 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,255 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 1,049 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 1,952 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 1,379 Apr-01-2024, 03:34 PM
Last Post: HerrAyas

Forum Jump:

User Panel Messages

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