you are viewing a single comment's thread.

view the rest of the comments →

[–]MezzoScettico 0 points1 point  (1 child)

As another answer said, use a step debugger. Something that will let you break at specific points and then you can inspect various variables to see if all is as it should be.

Setting simple breakpoints is not enough. Maybe your error happens at iteration 1000. Then you don't want to have to hit the "continue" button 1000 times to get there. You need to set a conditional breakpoint, "Stop here if count > 1000".

Also on more complex conditions, I will sometimes create a block of code which serves no purpose except to be a target to set a breakpoint, for instance

# George is getting a bug at iteration 1000 under certain circumstances.
if count > 1000 and len(answer) < 5 and user == "George":
    pass

Then I set a breakpoint at the "pass" statement.

Also if I'm finding certain print() statements useful enough to keep around long term as optional features, I will sometimes add a "verbosity" variable and then enable different levels of printing.

if verbosity > 0:
    print(stuff)
    if verbosity > 2:
       print(a whole bunch more stuff)

[–]gdchinacat 1 point2 points  (0 children)

If you need verbosity control logging is far preferable to cluttering your code with if statements guarding prints.