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.
A function by definition always returns something. Even if you don't specify it, there is an implicit return None at the end of a python function. You can check for a "return" with the inspect module.
To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.
If the return statement is without any expression, then the special value None is returned.
There are 2 places where we can see the method return value: In the Debugger Immediate window, using the $ReturnValue keyword. To open the Immediate window while debugging, choose Debug -> Windows -> Immediate (or press keyboard shortcut: Ctrl + Alt + I). In the Debugger Autos window.
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__
In pdb, when the function returns a ->'value'
is added at the end of the line with the representation of the returned value.
For example:
(Pdb) s
--Return--
> test.py(12)do_stuff()->'f'
-> return result
(Pdb) q
means do_stuff()
returned 'f'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With