How to modify same variable with different classes?

Viewed 81

I have following code block.

class Starter():
    def __init__(self, reference):
        self.reference = reference

    def change_ref(self):
        print("Got :", self.reference)
        self.reference = 1
        print("Now :", self.reference)

class Stopper():
    def __init__(self, reference):
        self.reference = reference

    def change_ref(self):
        print("Got :", self.reference)
        self.reference = 2
        print("Now :", self.reference)


class Controller():

    def __init__(self):
        self.main_reference = 0
        self.starter = Starter(self.main_reference)
        self.stopper = Stopper(self.main_reference)

controller = Controller()
controller.starter.change_ref()
controller.stopper.change_ref()

and it outputs the following :

Got : 0
Now : 1
Got : 0
Now : 2

I want the starter and stopper class to modify and reach to same object. So the output I am looking is the following :

Got : 0
Now : 1
Got : 1
Now : 2

What is the best way to do this with using three different classes? Rather than array usage I am curious if we can take advantage of classes. If possible I can also use nested classes. My only constraint is that Starter and Stopper classes must be separated from each other since in my project I am using them as QThread subclasses. Which leads them to override their run method in a different way.

2 Answers

Not sure it is the most elegant approach but using a list as reference do the job:

class Starter():
    def __init__(self, reference):
        self.reference = reference

    def change_ref(self):
        print("Got :", self.reference[0])
        self.reference[0] = 1
        print("Now :", self.reference[0])

class Stopper():
    def __init__(self, reference):
        self.reference = reference

    def change_ref(self):
        print("Got :", self.reference[0])
        self.reference[0] = 2
        print("Now :", self.reference[0])


class Controller():

    def __init__(self):
        self.main_reference = [0]
        self.starter = Starter(self.main_reference)
        self.stopper = Stopper(self.main_reference)

controller = Controller()
controller.starter.change_ref()
controller.stopper.change_ref()

Your example does not work since when you instantiate the Starter and Stopper classes you pass them an integer, and it is passed by value.

In order to make this work, an option is to pass the controller object itself to the Starter and Stopper constructors, so that they can access and modify its main_reference:

class Starter():
    def __init__(self, controller):
        self.controller = controller

    def change_ref(self):
        print("Got :", self.controller.main_reference)
        self.controller.main_reference = 1
        print("Now :", self.controller.main_reference)

class Stopper():
    def __init__(self, controller):
        self.controller = controller

    def change_ref(self):
        print("Got :", self.controller.main_reference)
        self.controller.main_reference = 2
        print("Now :", self.controller.main_reference)


class Controller():

    def __init__(self):
        self.main_reference = 0
        self.starter = Starter(self)
        self.stopper = Stopper(self)

controller = Controller()
controller.starter.change_ref()
controller.stopper.change_ref()
Related