In pdb (or ipdb), we can execute instructions and evaluate expressions using or commands : !p
p expression
Rate the expression in the current context and print its value.
[!] Statement
Execute a statement (single line) in the context of the current stack frame. An exclamation mark may be omitted if the first statement word is not like a debugger command. To set a global variable, you can prefix the destination command with the global command on the same line
So, for example, I can type p reddit.get_subreddits()during debugging in ipdb, and the code will be executed in the current context, and I will see the return value.
Is there a way to debug the execution of such expressions "manually"?
Basically I would like to do this s reddit.get_subreddits(), but it just executes the command stepand ignores the expression.
EDIT: A Trivial Example
Take this simple function:
import random
def get_value_for_weekday(weekday_index=None):
values = [10, 20, 20, 10, 30, 30, 30]
if not weekday_index:
return sum(values) / 7
return averages[weekday_index]
if __name__ == '__main__':
while True:
import ipdb; ipdb.set_trace()
get_value_for_weekday(random.randint(0, 7))
What is being tapped because of if not weekday_index(he needs to check weekday_index is not None.)
Suppose I notice that I am getting 10half the number of times I expect. So I added import ipdb; ipdb.set_trace()before calling the function to try and debug the code.
So, I am in the ipdb console, and I suddenly realized that maybe the problem is that I am missing 0 as weekday_index. I can test my hypothesis directly in ipdb:
ipdb> p get_value_for_weekday(0)
22
, , - , weekday_index=0.
get_value_for_weekday(0), , if.
, ipdb, script, , 0, script ipdb, ipdb step (s).
, s get_value_for_weekday(0) , p get_value_for_weekday(0)?
- ?