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