Dprint -- debugged print that is conditioned to global variables/object

If this simple feature is adopted by the Python next version it can help standardize and test code.
Many developer may rely on printing information as an alternative for more advanced development tools. Sometimes there can be some constraints and limits for using structured debugging framework.
The issue with print is that either it require to be commented when it is redundant or it will tedious lines to make conditional or require simple functions to wrap it and set condition.

Here bprint can come handy. dprint can be assosicate to one global boolean variable.. if its value is True .. dprint will print. Ohterwise it will not.

It is possible to make dprint to be customized through channels like dprint.var1 = True .. that will control all the dprint that are set with that variable.

the implementation of this idea is very simple and it can be done definitely as a separate basic project perhaps in few minutes of work.

But it will testing and open source community.. Because the channels of the dprint can be set for each version without affecting the code when the relevant channels are switched off. it can support automating tests as well (like loop of channels of debug then mine the logged text against expected values) without using opinionated specialized libraries for that ..

and it will be over structured to install and import a library just to do such basic feature.

Sounds like what you want is logging. What exactly are your proposing, what is the benefit over the stdlib logging module?

You can use if __debug__: print(...), and then use python -O to remove these prints.

1 Like

I don’t understand how this idea supports automated tests. If you’re printing things, then a human still has to check the results. You then can’t, say, run tests in a CI/CD pipeline and have a failure stop the pipeline progressing to the next stage. Also it’s time consuming and won’t scale as the number of tests grows.

It’s fun to explore ideas like this, and I’ve wanted something similar to this.

For me, I’d love to be able to run;

python myScript.py?dev=true

And in the code, have something like print.dev(“In Text to Speech function”), which would only print when dev/debug mode is on.

Certainly, I could make a better habit of including logging with log-levels earlier in the development process.

My usual flow is something like:

(1) Bash script gets too complex
(2) Open a new Python file, and write out features & functionality as comments
(3) Create function stubs from the comments
(4) Put a print statement inside the function stubs, basically a “I’m in X function, with these variables available to me”
(5) Start parsing/looping & sending variables between the functions - usually printing out the data as “f” formatted strings to the screen

Later, I end up deleting those print functions - but when debugging, I like when they are there. I’d love to keep a “skeleton crew” of print statements - essentially a “–verbose” mode for my app, and could see how having a toggleable print function would be useful, before bringing the “complexity” of logging/log-levels into the program. I say complexity, because I often have to look up exactly how to boot up logging, whereas print is always available to my brain’s muscle memory.

I’m not suggesting a solution here, but I empathize with the feature idea - would love to hear more about people’s workflows/suggestions/ideas on it.

It would absolutely be possible to write a wrapper around the Python command that implemented something like this - there’s no need for it to be built into the Python interpreter.

I agree, it’s fun to experiment with things like this. But it’s not likely to get added to the core distribution.

1 Like

Python has a Development Mode which can be enabled by python -X dev myScript.py or PYTHONDEVMODE=1 python myScript.py. You can check sys.flags.dev_mode flag.

1 Like