How to check if an object's fields have changed in Python?

hash, python

Solution

You can simply achieve that by overriding `__setattr__` class method.

In[6]: class Point(object):
           def __init__(self, x, y):
               self.x = x
               self.y = y
               self._changed = False

           def __setattr__(self, key, value):
               if key != '_changed':
                   self._changed = True
               super(Point, self).__setattr__(key, value)

           def is_changed(self):
               return self._changed

In[7]: p = Point(2,3)
In[8]: p.is_changed()
Out[8]: False
In[9]: p.x = 23
In[10]: p.is_changed()
Out[10]: True

Problem

I have class like this: ``` class Point(object): def __init__(self, x, y): self.x = x self.y = y ``` I create an instance of this class by: ``` p1 = Point(5, 10) ``` I want to know if any field of this class was changed. I suppose that there can be some kind of hash function which results I can compare. For example, in bash I can write `md5sum <<<"string"`, `md5sum <<<"string"` in order to get `x`, `y` respectively. Then I can compare `x` and `y` to find out if they are different or not. Is there an analogous method which works on Python's object?

Original source