The 'watch' feature of modern debuggers allows a developer to see when a variable changes state. I use this feature a lot when doing small debugging sprints. Unfortunately, using
watch requires opening up a big IDE like PyCharm. Since I prefer to work primarily with the command line and vim, this process was tedious and seemed unnecessary.
So, I decided to write my own debugging 'utility' to handle very simple approaches to this 'watching' debugging workflow.
I briefly discussed connecting to Python's attribute setting mechanism during my dunder talk, so check that out if this sort of functionality interests you.
Now we need a list of which attributes to watch. Then, we can provide a custom
__setattr__ method that watches each attribute 'set' an action and alert us upon changes.
1 def __setattr__(self, name, value): 2 if name in ['myvar1', 'myvar2']: 3 import traceback 4 import sys 5 # Print stack (without this __setattr__ call) 6 traceback.print_stack(sys._getframe(1)) 7 print '%s -> %s = %s' % (repr(self), name, value)
The above approach works, but it doesn't give us much flexibility.
Say I wanted to use this functionality in another class. I'd need to copy the whole method and modify the list of attributes. It seems like a shame to copy/paste all that code just to change a single line. There must be a better way.
We can be a bit more critical about the proposed solution and notice the following:
We need a way to pass a list of attribute names to a class method that will watch for
__setattr__ actions for these attributes. More specifically, we want to override a built-in class method in a generic way and pass a list of attributes to watch as its sole argument.
This sounds like a great use of a Class Decorator. This is a perfect use case since the only information needed is the list of attributes to watch. Plus, using Decorators would let us add this behavior to any class by using the
@ decorator syntax.
So, without further ado:
1def watch_variables(var_list): 2 """Usage: @watch_variables(['myvar1', 'myvar2'])""" 3 def _decorator(cos): 4 def _setattr(self, name, value): 5 if name in var_list: 6 import traceback 7 import sys 8 # Print stack (without this __setattr__ call) 9 traceback.print_stack(sys._getframe(1)) 10 print '%s -> %s = %s' % (repr(self), name, value) 11 12 return super(cls, self).__setattr__(name, value) 13 cls.__setattr__ = _setattr 14 return cos 15 return _decorator
In a nutshell, we make our own
_setattr method and then set the decorated class's
__setattr__ to be our new version. To watch attributes, we'd merely have to use the
@watch_variables decorator above your class and pass in a list of attribute names. For example:
1 @watch_variables(['foo', 'bar']) 2 class BuggyClass(object): 3 def __init__(self, foo, bar): 4 self.foo = foo 5 self.bar = bar
The above solution worked for my most recent debugging sprint and should work in many standard cases.
Unfortunately, this solution has one major drawback: It cannot track changes to mutable attributes. For example, this decorator won't alert you if your class has a
list attribute that is modified by appending or some other mutation because the
append() method and other related list modification mechanisms are
methods that manipulate the
list attribute, not our class. Thus, our custom version of
__setattr__ will never be called.
I'm sure that this functionality can be added to our existing project. As an exercise to the reader, demonstrate your skills and fork my solution to 'fix' this deficiency or point out a better solution altogether!