Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Variable names and contents as debugging tool; looking for emacs/Python shortcut

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.

like image 614
Schof Avatar asked May 11 '10 18:05

Schof


People also ask

How do you print variable names and values in Python?

Using f-strings in Python to print variables is the most commonly used method and I would personally recommend using this method. In this method, an 'f' is placed before the opening quotation mark of a string. Braces {} are placed around the names of variables that you are looking to print.

How do you print the variable name of an object in Python?

To print a variable's name:Use a formatted string literal to get the variable's name and value. Split the string on the equal sign and get the variable's name. Use the print() function to print the variable's name.

How do you check the value of a variable in Python?

Python variables store values in a program. You can refer to the name of a variable to access its value. The value of a variable can be changed throughout your program. Variables are declared using this syntax: name = value.


2 Answers

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(' .

like image 104
unutbu Avatar answered Oct 05 '22 12:10

unutbu


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.

like image 20
Olivier Verdier Avatar answered Oct 05 '22 14:10

Olivier Verdier