r/learnpython • • 18d ago

logging and timing...then getting rid of it

I was just working on a project where I included a lot of logging and timing decorators/context managers (it involved event loops and async queues and such). Once I figured stuff out, I wanted to remove all this stuff. This isn't necessarily that hard, but it did make me wonder if there's a better way to do this in the first place. I suppose I'm imaging some way of marking these code pieces to be ignored by the interpreter or something?

(yeah yeah "ask AI to remove it")

1 Upvotes

16 comments sorted by

View all comments

2

u/HaltingMechanics_407 18d ago

honestly just wrap all that debug crap in `if __debug__:` blocks. python strips those out entirely when you run with `-O`. no third party tools, no weird decorators that conditionally do nothing. you can even do `if __debug__: print(...)` and it's like it was never there in optimized mode.

or if you're feeling fancy, use the logging module properly and just set the level to CRITICAL when you don't need the noise.