Q:

Is it possible to decorate the __setatr__ method in python?

I'm trying to trigger a function when the contents of an object changes. I was trying to do this via decorating the setattr method in python, since this gets called when the = operator is used. 
I've tried decorating the setattr function with a @decorator, and this works, but I want to try and decorate it at runtime so I can decorate it per instance of the object if I want. I've tried the code below so far. 
class TestObject:
    pass

def on_object_change(func):
    def wrapper(*args, **kwargs):
        print("value changed %s - %s" % (args, kwargs))
        func(*args, **kwargs)
    return wrapper

obj = TestObject()
wrapper = on_object_change(obj.__setattr__)
obj.__setattr__ = wrapper

obj.one = 1
obj.__setattr__("two", 2)
setattr(obj, "three", 3)

I expect that the output would be:
value changed ('one', 1) - {}
value changed ('two', 2) - {}
value changed ('three', 3) - {}
But the actual output is:
value changed ('two', 2) - {}
The contents of obj have changed to, one, two and three. But the wrapped function was not called. I was just wondering if this is expected behaviour or not. And if there is another way to decorate the setattr function.

A:

I figured something out
def on_object_change(func):
    def wrapper(item, value):
        print("value changed %s - %s" % (item, value))
        func(item, value)
    return wrapper

class TestObject:
    def setattr(key, value):
        super().__setattr__(key, value)

    def __setattr__(self, key, value):
        self.setattr(key, value)

obj = TestObject()
obj2 = TestObject()

obj.setattr = on_object_change(obj.setattr)

obj.one = 1
obj2.two = 2

Since the setattr method is bound to the class, it was not possible to do this per instance. With the added bridge between these two functions it is possible to add a wrapper per instance of TestObject.
output obj.one = 1 -> value changed one - 1
output obj2.two = 2 -> 

Which is exactly what I need.

