-1

Is there any way that I can find the total difference in state between two epochs in a python session?

I'd like to detect changes to any global variables, including any arbitrarily-nested changes (such as reassignments to object attributes and to container elements).

The reason for this is to debug a complicated function which has an unexpected side-effect of interfering with the subsequent behaviour of another function. I'd like to find what changed between before and after that function invocation. (I'm hoping to be presented a summary of changes. I don't want to have to manually trace through all levels of subroutines, looking for variables that will not pass out of scope and are modified and are not subsequently restored to their original values.) I guess I want something akin to a diff of the memory heap, but at a higher level of abstraction. (This could also be helpful when debugging memory leaks.) Is there already anything in the standard library that can assist?

benjimin
  • 3,080
  • 24
  • 38
  • 2
    If you need this kind of debugging then your function is too complex. You should try to refactor before getting yourself stuck in endless debugging. – Klaus D. Sep 27 '19 at 04:31
  • @KlausD. I'm trying to debug someone else's function, and do not yet know what refactoring will be relevant. – benjimin Sep 27 '19 at 04:35

1 Answers1

1

Have you tried globals() to return a dictionary of globals?

Once you have that, there is some discussion about recursive diff here: Recursive diff of two python dictionaries (keys and values)

enerve
  • 73
  • 6
  • That gives you one module's globals. There can be lots, lots more. – user2357112 Sep 27 '19 at 04:50
  • @user2357112 if you want to also inspect another module's globals, import that module into the current global namespace. If you want to inspect all modules, import sys. For modules and class instances, probably just need to recurse into `__dict__` attribute. – benjimin Sep 27 '19 at 05:06