Debugging 101: Replace print() with icecream ic()

preview_player
Показать описание
Today we learn about the Python package called icecream, which you should use instead of print when debugging your code.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚

💼 Services 💼

🌐 Social Media & Contact 🌐
Рекомендации по теме
Комментарии
Автор

You could do this all much quicker by just inserting the built in python debugger with the line "import pdb; pdb.set_trace()" where you would normally put your print statement. It will then start the debugger in your terminal and you can view and even change all variables during execution. It also helps finding exactly where your code is breaking as you're stepping through the code line by line. I can't recommend it enough.

monkmcfunk
Автор

You know, from 3.10 onward you can essentially do this without packages by doing print(f"{variable=}").
Putting an = after the thing you are formating makes it so that it tells you the name/calculation together with the value.

davidrusca
Автор

Learnt something useful today. Thanks, this is something I will probably use in my classes as debugging is what I do 99% of the time.

doofsCat
Автор

One of the first things I do in any new language is create a debugging system. Helps to get you confortable with the syntax and environment, and gives you the tools up front to progress more efficiently.

This is nice, too.

thetruth
Автор

Not going to lie, I use A LOT of print statement to debug. This is very cool, learn something new and will start trying TODAY!

BillyChan
Автор

Thanks so much, I'm constantly learning new things from your videos and it's like this video was made for me. I was feeling specifically targeted when you were talking about using print(a) and print(type(a)), etc. 😄

Syfmuna
Автор

Interesting. Will check it out. The 'timing' of when the icecream output happens looks relevant and you didn't touch on the behaviour there. But a nice introduction. Thank you.

davidfell
Автор

Very useful. Don't worry about "professional" way. Log files and print statements are the way to debug large runtime programs.

RobertLugg
Автор

I would suggest skipping ice cream and going straight to python logging. It give much more control over how/what logging in emitted. I have found on many projects that starting from day 1 with using logging, instead of print is a great idea. The only thing I see that ic does that other don't is the quick ability to wrap a lines of code in ic and get a decent print that can help quickly debug issues. That said a debugger could also solve that use case.

benlpayne
Автор

I just found out about ice cream a couple of days ago. It was just what I needed when I needed it. Right now, I am developing in Flask, which doesn't print to the console, unless you flush. Ice cream takes care on that.

MegaJohn
Автор

Sounds like I'm calling goodbye() on print()! Thanks for the video mate, super handy

Tanger
Автор

Why the output for result 30 appears/printed before the output for IC? @5:22
[EDITED] I just saw that this happens on every run, the IC output always comes after the program runs. I'm curious to know why.

ConradoFonseca
Автор

Only after hearing it a dozen times did I realize that the function ic() is supposed to be a pun "I see" lol

vani
Автор

this is good to take debugging step by step, it reaches out to be complicated at the end, so I think it would be better to show the real level at the end, which is the built in debugger, where you can do all the debugging techniques ( breakpoints, tracing code line by line, viewing all variables..etc )

CodePhiles
Автор

Very useful! I will be using it from now on changing my 25 year old habit of using print()

StuartWoodwardJP
Автор

I am using the same method in order to reg logs but without icecream, just adding what is needed using strings to a txt file among with the date stamp. Wondering which version is more memory effective.

pythoneatssquirrel
Автор

This is very cool! Didn't know about it, but this looks super helpful. Thanks!

adamewing
Автор

Is there a way to configure ic so that it is virtually removed, so that you can leave them in the source code but not only does it not ouput anything, but it even doesn't slow it down?

bart
Автор

This is a nice showcase of icecream IC. Looks nice for some smaller projects. I do think, however, that there are better options to replace a good old print() statement, some of which are named in other comments. As for the pretty printing, if you add a bit more depth to some of those dictionarie, it could become bothersome to find what you are looking for. It is nice to be able to write the output to a file. Alternative for this includes the logging module which allows you to log on certain levels. Using logging together with some of the other builtin features might give you a nice solution that depends on standard library only.
It is convenient, however, to have everything with a simple function call.

dariusesterhuizen
Автор

Can I ask, why does it replace 10, 20 in the add function call to x:10, y:20? What does it mean and how does it differ from x=10, y=20. I can’t seem to find documentation or reference about this.

umbalaxx