Skip to content
Advertisement

How to trigger a method in 2 instances of the same class object in Python?

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()

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement