0

In RxPy, is there anything similar to INotifyPropertyChanged in .NET framework mentioned here? I'm trying to add an observer to an object, so that any property of the object changes, a function will be called.

Danny Wang
  • 429
  • 9
  • 24

1 Answers1

2

Try something like this:

class A(object):
    def __init__(self):
        self._my_attr = None
        self.property_changed = Subject()
    ...
    @property
    def my_attr(self):
        return self._my_attr

    @my_attr.setter
    def my_attr(self, value):
        if value != self._my_attr:
            self._my_attr = value
            self.property_changed.on_next(('my_attr', value))

a = A()
a.property_changed.subscribe(print)
a.my_attr = 1
a.my_attr = 3
mkzqeyns
  • 71
  • 2
  • 1
    Is there a way to do this for literal variables like variables holding string, list, or dictions - dictions more specifically. – Gary Jan 13 '20 at 18:03
  • 1
    It's kind of amazing ReactiveX for Python doesn't support detecting changes to properties considering that is the entire purpose of functional reactive programming. – ibrust Dec 08 '22 at 21:13