Why use IceCream to debug

Let’s look at an example where the average of three values is printed by the Python code. 

Note: This is just an example, so just concentrate where and for which variable the print() function is used rather than understanding the code.

Python3




# Python program to print average of three values
a = 3
b = 11
c = 42
  
for i in range(80, 83):
    a = a*i
    b = b*i
    c = c*i
    ans = (a+b+c)/3
    print(a)
    print(b)
    print(c)
    print(int(ans))


Output:

$ python gfg.py
240
880
3360
1493
19440
71280
272160
120960
1594080
5844960
22317120
9918720

When we print the values of a, b, c and ans for debugging purposes, it is difficult to identify which variable corresponds to which output. But what if there are hundreds of values in the output? IceCream is very helpful in these situations for debugging.

Below is the code for printing a, b, c, and ans using IceCream ic().

Note: This is just an example, so just concentrate where and for which variable the ic() function is used rather than understanding the code.

Debugging with ice cream in Python

Do you often use Python print() to debug your code? However, if you want to make a significant improvement, you could use IceCream which would make debugging faster, cleaner, and easier to read. ic(), which is short for IceCream prints both expressions/variable names and their values. ic() is faster to type than print(). The output is highlighted and printed in a structured format. If you want, you can include program context (filename, line number, and parent function) easily. You could effortlessly debug while solving competitive code problems and save a lot of time while debugging.

Similar Reads

Installation

To install the IceCream module, you need to open up your terminal or command prompt and type the following command:...

Why use IceCream to debug

Let’s look at an example where the average of three values is printed by the Python code....

Python program to print average of three values using ic()

...

Simplest Example

Python3 from icecream import ic    a = 3 b = 11 c = 42    for i in range(80,83):     a = a*i     b = b*i     c = c*i     ans = (ic(a)+ic(b)+ic(c))/3     ic(int(ans))...

Invoking Python Function using icecream module ic()

...

Multiple ways to print ic() in a function

Here’s a basic example of how to use the IceCream ic() function. Any data type and data structure that python print() supports can be used with IceCream ic()....

ic() helps in identifying the block of code that was executed

...

Using includeContext of icecream module

Here’s an example to use IceCream with the python function. The implementation of ic() for printing the return value of the function is the same as using the python print function....

Enable/Disable icecream module

...

Using configureOutput parameter in icecream module

Here are a few different approaches to include ic() in a function to get basically the same result....

Contact Us