Live data from Hacker News

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

github.com

91–100 of 271 posts

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

#92
This here is excellent:

    print(f"{d['key'][1]=}")
        d['key'][1]='one'
My immediate reaction was: How can I make this `form` easier to type. And that's what `Iceacream` does. Reviewed their code and realized they work with AST and inspection.

I recently started to learn (Common )Lisp. I realize how easy it would be to write a macro in Lisp that would implement the functionality of `Iceacream`, in a few lines of code.

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

#93
This is a really nice tool. But the fundamental reason most go for print is because its right there and that wins over other UX improvements or machinery. Python is a language where you can get a reasonably good debugger with a single line almost anywhere, still people reach for print()

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

#94
post #90
post #87

Instead of introducing a new library, why not just instruct your editor to do the heavy lifting for you? For instance, I've written a small Emacs function that asks for the argument to print in the Minibuffer and then inserts it once quoted, once unquoted. It's quite nice to have such a function for any programming language you use and bind it to the same keyboard short-cut.

For one, people not using emacs, or whatever editor it would be, can use it.

Sure, but why would anyone not use Emacs?!

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

#95

I promise the 15 minutes it takes to learn to use the debugger will save years of your life

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.

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

#96
post #90
post #87

Instead of introducing a new library, why not just instruct your editor to do the heavy lifting for you? For instance, I've written a small Emacs function that asks for the argument to print in the Minibuffer and then inserts it once quoted, once unquoted. It's quite nice to have such a function for any programming language you use and bind it to the same keyboard short-cut.

For one, people not using emacs, or whatever editor it would be, can use it.

Then instruct whatever is your favorite editor likewise.

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

#97
post #80

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

The old style printf from C is still the best formatting tool for output/debugging. The C++ style was just a distraction without introducing anything of real value. log4xyz has some nice features in terms of enabling/disabling at runtime, through a config, but ultimately, printf rules.

The value introduced by C++ was type safety. In C, it's way too easy for the format string to get out of sync with the type of the arguments, e.g.:

    printf("%d", my_long_var);
Might seem correct and work correctly on one platform, but fail on another. scanf() is arguably even worse since it can cause memory corruption.

These days compilers have diagnostics to catch those errors, but if you rely on those you can't use dynamic format strings, which means you're effectively using a subset of C with a stronger type checker than C. That's a pretty good state but it's definitely not "old style printf()"; old style printf() was insecure.

And don't get me started on the convoluted macro invocations necessary to correctly convert int32_t, int64_t, size_t and ptrdiff_t. And that's with the newest standard: IIRC there was no standard way to print long long in C, at the time when C++ already supported it.

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

#98
I am looking at the examples (and the comments here about f-strings), but neither fit my own usage of debugging with print. When I use print, it is mostly for two reasons. To see the structure of a dict (often with multiple layers of dicts inside), or to get dir(x) to see what methods and attributes of a library object, since the documentation is not always so forthcoming.

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

#100
wait isnt that just enforcing bad practice? doesnt python have proper debuggers with breakpoints ect? why in hell should i use a lib to print stuff just for debugging? i mean why not having a proper logging lib and pipe some statements to [debug] or whatever i dont get it.
Post reply on HN