When debugging, I sometimes find it useful to access the values of local variables within a class method without necessarily invoking pdb.set_trace()
.
class Calculator:
# ...
def add_and_format(x, y):
sum = x + y
return '{:.2f}'.format(sum)
If I want to access sum
following a call to add_and_format
, I could edit the code to be self.sum = x + y
and inspect it that way. In the case of many local variables, however, it would be simpler if I could use a @debug
decorator around the method or some debug_function()
class method to persist all locals variables as attributes. I guess I have in mind something basically equivalent to
def my_method(self, *args, **kwargs):
# ... method code
for k, v in locals().items():
setattr(self, k, v)
return result
I've seen this answer, which is a good partial solution but I'm unsure how to get it to work for this use case.