11

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.

Schof
  • 6,329
  • 5
  • 28
  • 38
  • 1
    After all this years and stumbling with this question again, I finally get it, what we needed all along were syntactic macros! ie. in [#julia-lang](http://stackoverflow.com/questions/tagged/julia-lang): `@show foo = 1.0 + 3im / 2` prints; `foo = 1.0 + (3im) / 2 = 1.0 + 1.5im`, where `@show` is a macro and can be used to anotate any expression (in Julia everything is an expression), you can see the code generated from the macro expansion here: https://git.io/vgDch I found this implementation of syntactic macros for Python: https://github.com/lihaoyi/macropy but Julia metaprograming is the best! – HarmonicaMuse Feb 13 '16 at 09:05

3 Answers3

10

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.currentframe and inspect.getouterframes), but I find this useful for cutting down on typing:

In utils_debug.py:

import inspect    
def pv(name):
    record=inspect.getouterframes(inspect.currentframe())[1]
    frame=record[0]
    val=eval(name,frame.f_globals,frame.f_locals)
    print('{0}: {1}'.format(name, val))

Then in your script.py:

from utils_debug import pv

With this setup, you can replace

print("a_variable_name: %s' % a_variable_name)

with

pv('a_variable_name')

Note that the argument to pv should be the string (variable name, or expression), not the value itself.

To remove these lines using Emacs, you could

C-x (   # start keyboard macro
C-s pv('
C-a
C-k     # change this to M-; if you just want to comment out the pv call
C-x )   # end keyboard macro

Then you can call the macro once with C-x e or a thousand times with C-u 1000 C-x e

Of course, you have to be careful that you do indeed want to remove all lines containing pv(' .

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Your way may not be very elegant, but I find it pretty useful, having the same need as Schof , that is avoid some redundant typing when debugging variables – Sébastien Nov 12 '11 at 17:40
  • I know this is an old answer, so things might've changed. But I stumbled upon this with Google so I will leave this here. Now you can directly do `frame=inspect.currentframe().f_back` which is a bit nicer and more clear :) – spalac24 Jan 12 '15 at 06:49
1

Don't do that. Use a decent debugger instead. The easiest way to do that is to use IPython and either to wait for an exception (the debugger will set off automatically), or to provoke one by running an illegal statement (e.g. 1/0) at the part of the code that you wish to inspect.

Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
0

I came up with this:

Python string interpolation implementation

I'm just testing it and its proving handy for me while debugging.

Community
  • 1
  • 1
HarmonicaMuse
  • 7,633
  • 37
  • 52