Live data from Hacker News

Icecream: Never use print() to debug again in Python

github.com

211–220 of 271 posts

Re: Icecream: Never use print() to debug again in Python

#211
post #133

Earlier quoted context omitted.

You said "click", I need to leave my keyboard. 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 d…

So basically tracepoints, without touching the program code.

But I'm already mucking with the program code most of the time so I'm not worried about touching it.

Re: Icecream: Never use print() to debug again in Python

#212
post #66

Earlier 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.

It's a way, it's hardly "the most correct way". Jeesh sometimes hackernews people. There are all kinds of ways to debug, and sometimes code has to run in different version of python. I find logging with various levels of "debug info" is best for me, but I hardly think that is "the most correct way"

Re: Icecream: Never use print() to debug again in Python

#213

I am always going to use print to debug in every programming language I can until the day I die.

Agreed, it's the simplest way to test and validate specific assumptions. Debuggers are useful tools, but it takes you just as long but usually longer to get to the same answer: is what I think is happening here actually happening here?

At least debuggers give you new powers, the posted above makes you install a library, import it all just so you can have slightly cleaner print statements... I'm not gonna go out of my way to import something and have to clean it up after just so my printed statement is better formatted. And if I needed the more powerful debugging, I'd use a debugger not this library.

Re: Icecream: Never use print() to debug again in Python

#214

I see print debugging and breakpoint debugging as two different tools, both very useful. Print statements are useful when you don’t know where to put a breakpoint, of course, but also when it’s important to see the real-time execution along with the debug logs. Breakpoints are of course insanely helpful when you know where to put them, but also can be peppered about suspect areas and used with steps to cover wider ra…

This isn't even breakpoint debugging though, this library simply makes your print statements "easier" to write, at the cost of having to install and import a library.

Re: Icecream: Never use print() to debug again in Python

#215
post #8

For an even easier-to-remember alternative, there’s q: https://github.com/zestyping/q All you need is `import q`. q works like a function (q(x)), like a variable (q|x and q/x, so you get different operator precedences) and like a decorator (@q), so it can be used in practically any circumstance for a quick debug print. Plus, the name sounds like you’re interrogating something.

I was wondering whether someone would notice that it's a clone of q! Thanks! :) q has the additional feature that you can decorate any function or method with `@q`, which causes invocations to be logged with arguments, return values, and exceptions. Really handy for tracing what's happening in your program.

A quick look at the intro page was a bit disappointing. You immediately get how ic works because their examples include the code and result.

Re: Icecream: Never use print() to debug again in Python

#216
post #136

Earlier quoted context omitted.

This is actually really cool. I was going to say that so many people miss the point of print debugging and as a consequence forget to shorten the import line as much as possible. from icecream import ic;ic() is 28 characters import q;q() is 12 characters print() is 7 characters

Why would the amount of characters possibly matter?

bc pgmrs r lazy af.

Seriously though... when I'm in debugging mode, speed and efficiency in completing the task is paramount. So, every character I save typing, the better!

Re: Icecream: Never use print() to debug again in Python

#217

Earlier quoted context omitted.

Sometimes sticking the debugger into the wheel makes stuff come flying over the handle bars in spectacular ways that have nothing to do with what you wish to observe. You might not even know which wheel to jam the debugger stick into, if the behaviour is complex. In these cases prints work well as a less intrusive way to get a rough idea of what is going on.

I don't understand your wheel analogy, sorry. > You might not even know which wheel to jam the debugger stick into, if the behaviour is complex. If you don't know where to put a breakpoint, how do you know where to put a print statement?

As someone who help teaches intro level CS class, debuggers can be too OP.

Re: Icecream: Never use print() to debug again in Python

#218
post #184

Earlier quoted context omitted.

What I imagine Macha is arguing for is that the cost of using print is extremely small, smaller at least than breakpoints. No one is saying breakpoints are useless, sometimes printing is 'cheaper' in time and effort in order to locate the region code of code in which using breakpoints is cheaper.

Yes, print() and breakpoints are different tools with different uses and there's cases where one is superior to the other. This is why some tools now offer logpoints, which are basically print() inserted via a breakpoint UI rather than in your code where you can forget to remove them

Oh please tell me about those tools.

Re: Icecream: Never use print() to debug again in Python

#219
post #136

Earlier quoted context omitted.

This is actually really cool. I was going to say that so many people miss the point of print debugging and as a consequence forget to shorten the import line as much as possible. from icecream import ic;ic() is 28 characters import q;q() is 12 characters print() is 7 characters

Why would the amount of characters possibly matter?

reduce the energy barrier to debugging

Re: Icecream: Never use print() to debug again in Python

#220
post #184

Earlier quoted context omitted.

Yes, print() and breakpoints are different tools with different uses and there's cases where one is superior to the other. This is why some tools now offer logpoints, which are basically print() inserted via a breakpoint UI rather than in your code where you can forget to remove them

Oh please tell me about those tools.

VS Code and Firefox Developer Tools are the two I'm aware of with actual support. Also some tools you can adhoc it as a conditional breakpoint by basically putting "print(whatever); return false" as the condition
Post reply on HN