r/Python • • Sep 19 '26

Discussion What’s a debugging technique that saved you hours and you wish you’d learned earlier?

I’m curious about the small debugging techniques that make a big difference when working on real projects.For developers with some experience, what debugging technique or habit has saved you the most time?It could be something simple that beginners often overlook.

What’s one debugging tip you wish someone had taught you when you were starting out?

151 Upvotes

159 comments sorted by

View all comments

Show parent comment

4

u/srcLegend Sep 19 '26

Doesn't it incur a performance penalty if there are logging lines everywhere, even if they're disabled by setting a lower verbosity level?

9

u/Schmittfried Sep 19 '26

If that’s a problem, you’re using the wrong language. 

2

u/srcLegend Sep 19 '26

I meant in general. Wouldn't that be true of all languages (short of preprocessor directives)?

4

u/nicholashairs Sep 20 '26

It would incur a penalty, but for most use cases it would be negligible, checking if a log level is activated should be a super fast evaluation. If you really are concerned about such performance then you should already be benchmarking and doing tricks to strip them out.

The one exception is if building your log line is expensive in which case you should put it in a guard that is fast:

# original 
self.logger.debug(expensive())

# optimised
if self.logger.enabled_for("DEBUG"):
    self.logger.debug(expensive())

3

u/DNSGeek Sep 20 '26

Also, use % string concatenation with the logging module. It has its own string evaluator, so if the level isn't going to be logged the string is never evaluated.

logging.debug("name=%s" % name)

1

u/RingularCirc Sep 21 '26

How exactly is it not evaluated?

2

u/glenbolake Sep 22 '26

u/DNSGeek had a typo. The syntax is supposed to be logging.debug("name=%s", (name,)), and it gets evaluated inside the logging module after a log level check.

2

u/DNSGeek Sep 22 '26

Yeah. You’re right. I was rushed and I put in the wrong syntax.

1

u/RingularCirc Sep 22 '26

Happens! (But it was soooo confusing lol.)

1

u/RingularCirc Sep 22 '26

Oh thanks, this unconfused me.

1

u/DNSGeek Sep 21 '26

The logging system looks at the level, e.g. “logging.debug”. If the current logging level is INFO or higher, then the logging system immediately returns without even looking at the string or doing the substitutions.

3

u/chief167 Sep 19 '26

No, well technically yes, but it's on the order of sub-milliseconds

2

u/IcecreamLamp Sep 19 '26

It's also trivial to later delete all lines starting with _logger.debug(…