6

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

2 Answers 2

10

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()
Sign up to request clarification or add additional context in comments.

12 Comments

I assume that va setter will called in first client, right? So does this solve the issue that the first client will be eliminated before this setter invocation return?
The setter is invoked immediately upon self.center.va = 1, before client.change exits, so it runs before the client goes away.
What if the client must leave while or before this invocation chain returns?
btw, i cannot sue property since I'm inheriting old style class.
You appear to have left a lot of important context out of your question.
|
6

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.

6 Comments

I'm using Twisted, and it seems that I cannot use new-style class. Can I still use this?
It is the part of core Python. You can use it with any flavor of Python. Made an edit with sample code
you said: " this __settattr__ function will be called." but __setattr__ will be called by who?
Did you checked the document linked to __setattr__()? Whenever a value is assigned to class's property,this function is called. For example, on doing self.va = 9, for assigning the value as 9, this function is called
Yes, I checked that out. So I assume that if I have this assignment: center.va = 9 in a client object, then the center object will invoke the __setattr__() and there's no function invocation chain between center object and client object, right?
|

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.