Python – How to see the return value from a function in the Python debugger, without an intermediate

debuggingpython

PDB (and other Python debuggers) have a simple way of viewing the value of any current variable, just by typing it in. However, sometimes I work with libraries that don't store their return values in intermediate variables.

Here's an example function:

def do_stuff(*args, **kwds):
     return f(*args, **kwds)

After I return from f, how do I see the return value? I could rewrite libraries after I download them to have an intermediate:

def do_stuff(*args, **kwds):
     r = f(*args, **kwds)
     return r

but it seems like there should be a better way.

Best Answer

You can look into a hidden __return__ local variable.

If I would forget it's exact name, I explore it by this:

(Pdb) sorted(locals().keys())
['__return__', 'xyz', ...]

EDIT: Related later answer with example of debugging with __return__