Live data from Hacker News

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

github.com

101–110 of 271 posts

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

#102
post #95

Earlier quoted context omitted.

Not necessarily. I know very well how to use a debugger, but nowadays I just prefer to use prints: it forces to you actually think about what's happening instead of just looking at it. It's also more useful in situations where putting a debugger actually changes behavior (high performance systems, parallel programs).

I would expect print-debugging to change the behavior in such cases too. Usually writing to stdout is behind some synchronization so using print debugging will drop performance and make program less parallel.

You can change where to put the prints if they do change the behavior (which incidentally also helps in understanding what's happening). You can print only if a condition happens, or only before/after critical sections, or print counters that you compute in the fast path. With the debugger you're always taking the overhead in and breakpoints will always change the flow of the program.

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

#103
post #88

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 make it a bit prettier by adding spaces: >>> print(f"{d['key'][1] = }") d['key'][1] = 'one' It works with any expression you like, not just variables: >>> print(f'{np.sin(np.pi/4.) = }') np.sin(np.pi/4.) = 0.7071067811865475

> It works with any expression you like

Also assignment expressions? :)

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

#105

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.

> Why don't people just use a debugger? I only speak for myself, but I find that time spent inside a debugger is "lost", and bound to be re-spent again and again if you ever find new problems with the same code. I pretty much prefer to add assertions, logging and comments to my program, that will stay there and make further re-reads easier and more useful. As a matter of principle, I never use a debugger (except for…

+1 Agreed. Unless you can immediately see the problem from back-trace and local variables, stepping through with the debugger is often a waste of time compared to adding some logging.

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

#106
post #88

Earlier quoted context omitted.

You can also make it a bit prettier by adding spaces: >>> print(f"{d['key'][1] = }") d['key'][1] = 'one' It works with any expression you like, not just variables: >>> print(f'{np.sin(np.pi/4.) = }') np.sin(np.pi/4.) = 0.7071067811865475

> It works with any expression you like Also assignment expressions? :)

    >>> print(f"{(yes:='yes')=}")
    (yes:='yes')='yes'

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

#107
post #6

I am sorry, but I am not going to pull in an external dependency just for this, especially not with the power of fstrings in newer python versions.

You're just being difficult. It's easy, you just setup these 10 Node.js micro services in docker, setup your database, the cache. Then my friend, you can really debug with beautiful print messages. Wait until someone creates a $10/user SaaS service out of it.

[deleted]

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

#110
post #104

This looks cool and all, but why is it called Icecream? I know naming abstract stuff is hard but it feels like this lends itself to a more descriptive name. "ic()" tells me nothing about what the function does.

I think it's a (confusing) two-layer pun.

When read phonetically as letter names, the name "ic" sounds like "I see".

"ic" also is an initialism for "ice cream".

Everyone loves ice cream, and so another low-meaning cutesy-poo pun-ishment of a name was born.

I think. Pure speculation here.

Post reply on HN