Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Value Changed in Class B calls function in Class A to update object of class A

I want to call a function from a class A inside another class B. However, it should be called for an object of A. I mean if I have something like this:

class A:
         
    def __init__(self, ....):
        self.valuechanged=False
        
        # do something
        objectfromb=B()
        self.somearray.append(objectfromb)
    
    def updateevent(self):
        self.valuechanged=True
        # do some things if update event triggered
        

class B:
    def __init__(self,...):
        self.somevalue=0
        self.someothervalue=1
        # do something
    def updatesomevalue(self,somenewvalue):
        self.somevalue=somenewvalue
        # !!! HERE SHOULD BE A CALL TO CLASS A FUNCTION updateevent

And in my code I use the classes like this:

a=A()

Then i would have a list somearray in a (a.somearray) which contains an object of B. So if I want to update this object B with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a.somearray[0].updatesomevalue(10)

Then there should not only be a new value for a.somearray[0].somevalue but also the function update event of class A should trigger changing a. How can I do that?

>Solution :

There are two ways I can think of to achieve this without invoking any special magic.

The first is to have objects of type B know what object A they belong to so that they can call updateevent on it. This isn’t a way I’m generally a fan of as there’s extra admin work to do when moving instances of B between instances of A and such. If that’s not a concern then it may be the best way. You’d do that something like this (with a method on A to create a B and set the correct parent for convenience):

class A:
    valuechanged=False
    somearray=[]

    def add_b(self):
        b = B(self)
        somearray.append(b)
        return b
    
    def updateevent(self):
        self.valuechanged=True
        

class B:
    somevalue=0
    someothervalue=1

    def __init__(self, parent):
        self.parent = parent

    def updatesomevalue(self,somenewvalue):
        self.somevalue=somenewvalue
        self.parent.updateevent()

The second is to provide a method on A that does both tasks. This is only suitable if 1) you know A will always contains instances of B and only B and 2) B‘s interface is relatively small (to avoid providing lots of methods of this type on A). You would implement this as something like:

class A:
    valuechanged=False
    somearray=[]

    def updatesomevalue(self, index, new_value):
        somearray[index].updatesomevalue(new_value)
        self.updateevent()
    
    def updateevent(self):
        self.valuechanged=True
        

class B:
    somevalue=0
    someothervalue=1

    def updatesomevalue(self,somenewvalue):
        self.somevalue=somenewvalue

Something I haven’t addressed is that somearray, somevalue, etc are all being created as class attributes in your example (i.e. they will be shared among all instances, instead of each instance having its own ones). This is likely not what you wanted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading