Skip to content

Instantly share code, notes, and snippets.

@durden
Created November 15, 2012 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durden/4081514 to your computer and use it in GitHub Desktop.
Save durden/4081514 to your computer and use it in GitHub Desktop.
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)
traceback.print_stack(sys._getframe(1))
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