Icecream: Never use print() to debug again in Python
121–130 of 271 posts
Re: Icecream: Never use print() to debug again in Python
#122Party on! I applaud this effort. As a UI/UX engineer I can say that there is no one debugging UI/UX experience to fit all. Of course there is the active/passive division between active debugging and logging. There are further usability divisions in both of those. As this is a passive/logging debugging scheme I'm sure there are those in the passive debugging community who will find this helpful, if not vocal.There is…
Re: Icecream: Never use print() to debug again in Python
#123You can do the same thing with Python 3.8+ by using f-strings and just appending "=" to the variable name: >>> print(f"{d['key'][1]=}") d['key'][1]='one'
python3 myscript.py --enableFstringDebugging=true
So that I could get rid of lots of conditional statements, e.g.: if debug:
print (f"some useful debugging info")Re: Icecream: Never use print() to debug again in Python
#124Earlier quoted context omitted.
Absolutely, I don't understand why using print() or its equivalent in other languages is looked down upon. It's quick way to narrow down the "area of search" before bringing in the big guns.
How is changing code simpler than literally clicking on the line number to set a breakpoint?
Debug printing also allows you to debug programs running in environments where you can't attach a debugger. For example, maybe halting the program causes the bug not to trigger. Or it's a remote system where you cannot attach a debugger for various reasons. Or the bug only happens in the optimized build, which in say C/C++ can make it quite tedious to walk through with a debugger.
Most of the time though I use print as "proactive debugging". Having detailed logs available is gold when customer calls with a blocking issue.
Re: Icecream: Never use print() to debug again in Python
#125> prints both its own arguments and the values of those arguments. That's not enough. Almost always I also want the argument type . Occasionally a unique (i.e. as precise as possible) sortable (i.e. with leading zeros) time stamp also comes useful.
Not just with leading zeros but also written in the YYYY-MM-DD-HH-... format.
Re: Icecream: Never use print() to debug again in Python
#126I promise the 15 minutes it takes to learn to use the debugger will save years of your life
Re: Icecream: Never use print() to debug again in Python
#127Re: Icecream: Never use print() to debug again in Python
#128Earlier quoted context omitted.
TBF if you always run your programs in pycharm and use its debugger, you can trivially use non-suspending "evaluate and log" breakpoints instead of print.
But prints work equally well in any environment. I can remove prints by just checking out the latest version of the file.
GP say they "have debugger set in pycharm and use it all the time". So under the assumption (explicitly made in my comment) that they're always using PyCharm to run their program, that's not a concern.
> I can remove prints by just checking out the latest version of the file.
Thereby losing all the changes you've made while observing program behaviour, which may be less than desirable.
Meanwhile it's just as easy if not easier to disable or delete breakpoints from the View Breakpoints pane / window: https://i1.wp.com/cdn-images-1.medium.com/max/800/1*0wAP-w-a... you can just uncheck the "Python Line Breakpoints" box, or select all breakpoints and [Delete] them.
Re: Icecream: Never use print() to debug again in Python
#129Earlier quoted context omitted.
Absolutely, I don't understand why using print() or its equivalent in other languages is looked down upon. It's quick way to narrow down the "area of search" before bringing in the big guns.
How is changing code simpler than literally clicking on the line number to set a breakpoint?
Generally when I am coding I auto-run the tests on save. This means that to printf-debug I just add a message or two (and if I am coding I might already have a couple of useful ones lying around) and save. Then in less than a second I have a trace trough my program in the terminal. If I want to inspect a different variable I just add another print and run again.
With a debugger I need to kill my auto-run command, run the program, set breakpoints, type to see what variables I want to inspect, maybe switch stack frames, maybe step through a bit.
In my mind printf is like an automated debugger. I just put the info I want to see into the program and it does all of the printing for me. And when I find the problem I can just fix it and I am back to my edit-test cycle.
I'm not saying that there are no use cases for a debugger. For example I find variable-modification breakpoints very useful. As you mentioned if your edit-run cycle is slow then it may be faster to inspect a new variable in the debugger than adding another print statement. But when I just want to inspect values in my program I find printf sufficient and lower overhead. I'm sure part of my problem is that because I rarely use a debugger I am not as efficient, but I also think that printf-debugging is a very effective workflow for a wide variety of issues.
Re: Icecream: Never use print() to debug again in Python
#130Earlier quoted context omitted.
You can also use the breakpoint() introduced in py3.7 via PEP533 https://www.python.org/dev/peps/pep-0553/
Bump. This is the most correct way to debug python programs VS using print statement.