Debugging with ice cream in Python
Last Updated :
26 Oct, 2022
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.
Installation
To install the IceCream module, you need to open up your terminal or command prompt and type the following command:
$ pip install icecream
After installing the IceCream module, we are ready to use IceCream to debug Python code efficiently.
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.
Python program to print average of three values using ic()
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))
Output:
$ python gfg.py
ic| a: 240
ic| b: 880
ic| c: 3360
ic| int(ans): 1493
ic| a: 19440
ic| b: 71280
ic| c: 272160
ic| int(ans): 120960
ic| a: 1594080
ic| b: 5844960
ic| c: 22317120
ic| int(ans): 9918720
Explanation: Here, ic() is used between the code to print out the values in a structured and simpler method. We are easily able to identify the values of a, b, c and ans in the output.
Simplest Example
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().
Python3
# Python program to print different
# types of datatypes
# and data structures using ic()
from icecream import ic
NUMBER = 2008
FLOAT = 95.47
BOOLEAN = True
STRING = "GFG"
LIST = [1, 2, 3, 4, 5]
DICT = {"gfg": "geeksforgeeks", "founder": "Sandeep Jain", }
ic(123)
ic(NUMBER)
ic(FLOAT)
ic(BOOLEAN)
ic(STRING)
ic(LIST)
ic(DICT)
Output
ic| 123
ic| NUMBER: 2008
ic| FLOAT: 95.47
ic| BOOLEAN: True
ic| STRING: 'GFG'
ic| LIST: [1, 2, 3, 4, 5]
ic| DICT: {'founder': 'Sandeep Jain', 'gfg': 'geeksforgeeks'}
Invoking Python Function using icecream module ic()
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.
Python3
# Python program to implement ic()
# on functions
from icecream import ic
def square(i):
return i*i
ic(square(4))
Output:
$ python gfg.py
ic| square(4): 16
Explanation: Here, ic() prints the function with the argument that is being passed and the return value on the right.
Multiple ways to print ic() in a function
Here are a few different approaches to include ic() in a function to get basically the same result.
Python3
# Python program to understand different
# ways to print using ic()
from icecream import ic
def multiply1(i, j, k):
ic(i)
ic(j)
ic(k)
return i*j*k
def multiply2(i, j, k):
ic(i, j, k)
return i*j*k
def multiply3(i, j, k):
return ic(i)*ic(j)*ic(k)
ic(multiply1(1, 2, 3))
ic(multiply2(1, 2, 3))
ic(multiply3(1, 2, 3))
Output:
$ python gfg.py
ic| i: 1
ic| j: 2
ic| k: 3
ic| multiply1(1, 2, 3): 6
ic| i: 1, j: 2, k: 3
ic| multiply2(1, 2, 3): 6
ic| i: 1
ic| j: 2
ic| k: 3
ic| multiply3(1, 2, 3): 6
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.
Using includeContext of icecream module
By configuring output as includeContext = True, the output will also print the parent function, filename, and the line number of the ic() which is executed.
Python3
# Python program to configure
# prefix as parent function,
# filename, and the line number
from icecream import ic
ic.configureOutput(includeContext=True)
def square(i):
return ic(i)*i
NUMBER = 2008
ic(NUMBER)
ic(square(23))
Output:
$ python gfg.py"
ic| 9.py:11 in <module>- NUMBER: 2008
ic| 9.py:7 in square()- i: 23
ic| 9.py:12 in <module>- square(23): 529
Enable/Disable icecream module
With ic.disable() and ic.enable(), respectively, the output of ic() can be completely turned off and then turned back on.
Python3
# Python program to enable/disable ic()
from icecream import ic
ic.disable()
ic(1990)
ic("Hello World")
ic.enable()
ic("GFG")
ic(2008)
Output:
$ python gfg.py
ic| 'GFG'
ic| 2008
Note: Here lines ic(1990) and ic("Hello World") are not getting printed.
Using configureOutput parameter in icecream module
IceCream module also enables you to configure a custom prefix. In the below example, we are going to learn how to configure the output prefix as time using a function.
Python3
# Python program to configure
# output prefix as time
from datetime import datetime
from icecream import ic
import time
ic("gfg")
ic.configureOutput(prefix="GFG ")
ic("gfg")
def time_format():
now = datetime.now()
return f'{now.strftime("%H:%M:%S")} --> '
ic.configureOutput(prefix=time_format)
for i in range(1, 6):
time.sleep(1)
ic(i)
Output:
$ python gfg.py
ic| 'gfg'
GFG 'gfg'
02:02:41 --> i: 1
02:02:42 --> i: 2
02:02:43 --> i: 3
02:02:44 --> i: 4
02:02:45 --> i: 5
Explanation: In the output, line 1 shows the default output. Line 2 has a prefix 'GFG' which was configured in the code. From the 3rd line onwards, we were able to configure output with prefix time. The time shows a delay of 1 second as time.sleep() delays it as shown in the code. Finally, remove all the ic() after the debugging is complete.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read