Live data from Hacker News

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

github.com

111–120 of 271 posts

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

#112
post #60

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

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?

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

#113

Why don't people just use a debugger? It boggles my mind that people live with print style debugging. Depending on the project, it can be such a timer waster and just plainly worse in all aspects. Especially in Pycharm for me, it is incredibly easy. Even when running over ssh in remote computers I can use my debugger.

It depends on the situation. In general, debuggers give you much more contextual information, but using a debugger is extremely slow. To see what's going on, you have to step through your program, one line at a time, until the interesting thing happens. That can take ages. If you want to see how a specific function is behaving and it's called 100 times, it's a lot easier to look at a printed log of the 100 calls and…

> you have to step through your program, one line at a time

You can set more than one breakpoint at a time. Hitting 'c' will get you to the next breakpoint, no matter where it is.

> and step through it over and over

No, you don't (shouldn't) do that. You write a conditional breakpoint which activates when something interesting happens.

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

#114
post #83
post #60

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

> It's quick way to narrow down the "area of search" before bringing in the big guns. What are the big guns? with a debugger, I can stick a breakpoint and look at the entire state of everything. Given we're talking about Python, in pycharm [0] you can even execute your print statements in the debugger if you so wish. If you get the location wrong, or want to see what's going on elsewhere you can just continue executi…

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.

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

#115
post #66

You 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'

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.

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

#116
post #60

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

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.

The same thing happens all over Software really. Just because a tool is powerful is looked up as superior, or better.

The main argument that I have seen is that in print debugging you are relying on the program being executed in a non-descriptive/non-declarative fashion.

I legitimately believe print debugging is incredibly powerful (With a simple print I can check if a function is being called, how many times, if the value has the value I expected and the only requirement I need is to be able to see the stdout of the process. I say that is fantastic!

The real world is all about cost analysis. How much value can I get from a tool vs setup and running cost. The cost of print debugging is incredibly small.

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

#117

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?

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

#118

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

I am the same, it's the most easy. Very interesting is that in the laravel php world currently an interesting product is gaining momentum: Ray (https://myray.app/)

So basically it's dump with a lot of neat extras and instead of looking at the console of the script, or the website you are printing on you push this to a little desktop application, from every of your languages you are using. Something like log collection for everything on your desktop.

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

#119
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.

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

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

#120

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

Hmmm I would have thought that too, but recently I’ve been using byebug, which has changed my mind. Being able to throw in ‘byebug’ on a line, then catch execution at that point in another terminal (using byebug remote) and then check variables at that point, is a game changer. Saves so much time compared to looking for printed statements in the output, and then trying again.
Post reply on HN