Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to trigger function in another object when variable changed. Python

Tags:

python

As far as I know, this is like an Observer pattern. Scenario: A Center object keeps a list (queue) of all its clients. I'm using Twisted.

  1. One of client objects changes a variable in center object OR notify the center to change the variable,
  2. and then the center object detects the change immediately;
  3. then as soon as the detection, the center object invoke some function of next object in queue
  4. After the client changed the variable, the client object will be eliminated. The center will take care of next client object. So I imagine there's no any function chain between these objects. So it's a little bit different from observer pattern. (How to address this issue? Correct me if I'm wrong.)

following code is just for demo only:

    class client():
        def change(self):
            self.center.va = 1

        def inqueue(self):
            self.center.queue.enqueue(self)

        def function(self):
            pass

    class center():
        def __init__(self):
            self.queue = None
            self.va = 0

        ####  When the self.va changes, this func will be invoked
        def whenChanged(self):
            next = self.queue.dequeue()
            next.function()
like image 358
Henry Avatar asked Sep 27 '16 16:09

Henry


People also ask

How to use a variable from another function in Python?

How to use a variable from another function in Python: The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object.

How do you assign a variable to a function?

The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object. This variable can now be used anywhere inside or outside any function.

How to override setattr () function in Python?

Whenever a property of class is changed, setattr () function is called. You can override this by defining __setattr__ (self, property, value) function in your class. You need to make your required function call within this __ setattr__ ().

What is the relationship between a variable and a function?

The variable can be assigned to the function object inside the function body. So the variable exists only after the function has been called. Once the function has been called, the variable will be associated with the function object.


2 Answers

Make va a property.

class Center():
    def __init__(self):
        self.queue = None
        self._va = 0

    @property
    def va(self):
        return self._va

    @va.setter
    def va(self, value):
        self._va = value
        self.whenChanged()

    def whenChanged(self):
        next = self.queue.dequeue()
        next.function()
like image 151
chepner Avatar answered Nov 03 '22 00:11

chepner


Whenever a property of class is changed, setattr() function is called. You can override this by defining __setattr__(self, property, value) function in your class.

You need to make your required function call within this __ setattr__(). Below is the sample example based on your requirement:

class Centre(object):
    def __init__(self):
        self.queue = None
        self.va = 0

    def whenChanged(self):
        next = self.queue.dequeue()
        next.function()

    def __setattr__(self, key, value):
        self.key = value
        self.whenChanged()  # <-- Your function

Whenever you will attempt to change the value of any of class's property, this __settattr__ function will be called.

like image 26
Moinuddin Quadri Avatar answered Nov 02 '22 23:11

Moinuddin Quadri