I find myself adding debugging “print” statements quite often — stuff like this:
print("a_variable_name: %s" % a_variable_name)
How do you all do that? Am I being neurotic in trying to find a way to optimize this? I may be working on a function and put in a half-dozen or so of those lines, figure out why it’s not working, and then cut them out again.
Have you developed an efficient way of doing that?
I’m coding Python in Emacs.
Sometimes a debugger is great, but sometimes using print statements is quicker, and easier to setup and use repeatedly.
This may only be suitable for debugging with CPython (since not all Pythons implement
inspect.currentframeandinspect.getouterframes), but I find this useful for cutting down on typing:In utils_debug.py:
Then in your script.py:
With this setup, you can replace
with
Note that the argument to
pvshould be the string (variable name, or expression), not the value itself.To remove these lines using Emacs, you could
Then you can call the macro once with
C-x eor a thousand times with
C-u 1000 C-x eOf course, you have to be careful that you do indeed want to remove all lines containing
pv('.