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.
Contact Us