0

I had 2 instances (st0, st1) of the same class (State) in which has 2 methods. Somehow when st0.loc_st() is called, I want that st1.rem_st() to be automatically triggered. How can I achieve that purpose with Python OOP? Sorry the code looks a bit naive, but it's a simplified model saves the reading effort.

import random
class State(object):

    def __init__(self) -> None:
        self.s = ['S0', 'S1', 'S2']

    def loc_sc(self):
        # how to trigger a remote method in different class?
        print (self.s)

    def rem_sc(self):
        random.shuffle(self.s)
        print (self.s)

if __name__ == '__main__':
    st0 = State()
    st1 = State()
    # how can I trigger st1.rem_sc() automatically 
    #  when st0.loc_sc is called  
    st0.loc_sc()
5
  • 1
    What if there are 3 or more instances, should it be called in all of them? Commented Aug 15, 2021 at 0:36
  • 2
    What is the purpose of triggering the other method? This sounds like you want to solve a problem, but you're asking how to make your idea for a solution work. The best answer is probably to reevaluate your whole architecture and change it to something better suited to the problem. This is called an XY problem, if you know that terminology. Commented Aug 15, 2021 at 0:37
  • 1
    What you probably should have is another class that holds a list of objects, and calls the method on all of them in a loop. Commented Aug 15, 2021 at 0:42
  • I am using Python to model a H/W problem. I know it's not an ideal use mode and looks like a much twisted solution. However, in H/W there are so many such self-intervened problems like 2 FSM in 2 modules correlated each other. Commented Aug 15, 2021 at 1:03
  • @LouisCloete, thanks for the comments. I agreed with you, some time, I need to re-evaluate my whole architecture, if I eventually find it a dead road. But now I do wish to explore my current possible solution. As I mentioned, the questions come from H/W modeling. Commented Aug 15, 2021 at 11:55

2 Answers 2

0

If you want the method of every instances to be called, you can just add a list to the class and iterate over all the instances and call their method:

import random
class State(object):
    instances = []

    def __init__(self) -> None:
        self.in_trigger = 0
        self.ex_trigger = 0
        self.s = ['S0', 'S1', 'S2']
        State.instances.append(self)
    
    def loc_sc(self):
        # how to trigger a remote method in different class?
        print (self.s)
        for i in State.instances:
            i.rem_sc()
    
    def rem_sc(self):
        if self.ex_trigger:
            random.shuffle(self.s)
            print (self.s)
    
if __name__ == '__main__':
    st0 = State()
    st1 = State()

    st0.loc_sc()

Or you could add an array of instances to the class constructor. When the method gets called on an instance you could then iterate over all the instances in that array and call the method:

import random
class State(object):
    def __init__(self, instances=[]) -> None:
        self.in_trigger = 0
        self.ex_trigger = 0
        self.s = ['S0', 'S1', 'S2']
        self.instances = instances
    
    def loc_sc(self):
        # how to trigger a remote method in different class?
        print(self.s)
        for i in self.instances:
            i.rem_sc()
    
    def rem_sc(self):
        if self.ex_trigger:
            random.shuffle(self.s)
            print (self.s)
    
if __name__ == '__main__':
    st1 = State()
    st0 = State([st1])

    st0.loc_sc()

In case you want to initialize st0 first, you could either add an addInstance method to the class, which would add a instance to the instances array of that instance:

import random
class State(object):
    def __init__(self, instances=[]) -> None:
        self.in_trigger = 0
        self.ex_trigger = 0
        self.s = ['S0', 'S1', 'S2']
        self.instances = instances
    
    def loc_sc(self):
        # how to trigger a remote method in different class?
        print(self.s)
        for i in self.instances:
            i.rem_sc()
    
    def rem_sc(self):
        if self.ex_trigger:
            random.shuffle(self.s)
            print (self.s)

    def addInstance(self, instance):
        self.instances.append(instance)
    
if __name__ == '__main__':
    st0 = State()
    st1 = State()
    st0.addInstance(st1)

    st0.loc_sc()

Or you could also add a reference to the constructor, so that the instance itself gets added to the instances of that reference.

import random
class State(object):
    def __init__(self, reference=None) -> None:
        self.in_trigger = 0
        self.ex_trigger = 0
        self.s = ['S0', 'S1', 'S2']
        self.instances = []
        if reference != None: reference.instances.append(self)
    
    def loc_sc(self):
        # how to trigger a remote method in different class?
        print(self.s)
        for i in self.instances:
            i.rem_sc()
    
    def rem_sc(self):
        if self.ex_trigger:
            random.shuffle(self.s)
            print (self.s)
    
if __name__ == '__main__':
    st0 = State()
    st1 = State(st0)

    st0.loc_sc()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @drip for the detailed explanation and examples. I like the last solution as it's very cool :) However, my intention was to trigger st1.rem_sc() whenever st0.loc_sc() is called, not to loop all the methods in st0. Sorry, forget about ex_trigger variable, and it's confusing. I will modify my question to make it clearer.
0

Two instances don't know each other. So this is not a recommended case, but if you really want to do it, please restore all your instances in a list, when you want trigger one of them, just do a loop to find it. Here is the answer! may help you.

1 Comment

The solution in the link is very inspiring though it was semi-different question to mine, which data is bi-directional (loop back and loop forward). That answer is a data forwarding pipeline. Anyway, it helps with great solutions. :)

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.