Skip to content

Instantly share code, notes, and snippets.

Created November 15, 2012 21:44
What would you like to do?
Watch when variables change in your class
def watch_variables(var_list):
"""Usage: @watch_variables(['myvar1', 'myvar2'])"""
def _decorator(cls):
def _setattr(self, name, value):
if name in var_list:
import traceback
import sys
# Print stack (without this __setattr__ call)
print '%s -> %s = %s' % (repr(self), name, value)
return super(cls, self).__setattr__(name, value)
cls.__setattr__ = _setattr
return cls
return _decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment