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

To determine which block of code was executed, you could use ic() to print the line that was executed. If no arguments are passed in ic(), it will print the line of code that was executed with the parent function, filename and time.

Python3




# Python program to print parent 
# function, filename, 
# and the line number as prefix
from icecream import ic
  
def gfg(value):
    ic()
    if value == True:
        ic()
        return "GFG"
    else:
        ic()
        return -1
ic(gfg(True))


Output:

$ python gfg.py
ic| new.py:6 in gfg() at 18:42:14.916
ic| new.py:8 in gfg() at 18:42:14.917
ic| gfg(True): 'GFG'

Explanation: In the output, Line 1, 6 in gfg() indicates that the program executes the gfg() function and encounters ic() on line 6. Line 2, 8 in gfg() indicates that the program executes the if block and not the else block, as the argument value is passed ‘True’ and encounters ic() on the 8th line of the code. It also prints the time beside it.

ic() default configuration to print the filename, line number, and time as a prefix is false when an argument is passed. But it can be enabled by configuring the output, which we will learn next.

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