Debugging in Python: Replace print() with ic() and Do It Like a Pro | by Kevin Meneses González | The Pythoneers | Medium


This article compares Python's `print()` function with the `ic()` function from the IceCream library for debugging, highlighting the latter's advantages in clarity and organization.
AI Summary available — skim the key points instantly. Show AI Generated Summary
Show AI Generated Summary

Introduction:

A few months ago, while working on a data analysis project in Python, I faced a recurring error. Although I reviewed the code repeatedly, I couldn’t identify the problem. Like any respectable programmer, I resorted to the good old print() to understand what was happening. However, as I printed endless lines of output in the terminal, I realized how ineffective this method was. That’s when I discovered IceCream, a library that helped me improve the debugging process exponentially, making it cleaner, more organized, and professional

Comparison: print() vs ic()

We’ve all used print() to debug code, but this technique has its limitations. Although it’s easy to implement, it often becomes confusing when working with more complex functions and data structures. This is where IceCream comes in, with its ic() function, a tool specifically designed for debugging with additional features.

Basic Example with print():

def add(x, y):    return x + y# Trying to debug with print()print(add(10, 20))  # Output: 30print(add(30, 40))  # Output: 70

The problem with this approach is that when the output becomes extensive, it’s not clear which values belong to each result. Here, you must manually associate each result with the operation that generated it.

The Same Situation with ic():

from icecream import ic# Using ic() to debugic(add(10, 20))ic(add(30, 40))

Output

ic| add(10, 20): 30ic| add(30, 40): 70

As you can see, IceCream not only prints the result of the operation but also shows which function was called, along with the parameters passed. This greatly simplifies the debugging process, especially when you have multiple function calls with similar outputs.

Advantages of Using ic()

1. Detailed Operation Information

With ic(), you don’t just see the result; you also see the operation that was performed. This eliminates the need for using f-strings or manual comments to know…

Was this article displayed correctly? Not happy with what you see?

Tabs Reminder: Tabs piling up in your browser? Set a reminder for them, close them and get notified at the right time.

Try our Chrome extension today!


Share this article with your
friends and colleagues.
Earn points from views and
referrals who sign up.
Learn more

Facebook

Save articles to reading lists
and access them on any device


Share this article with your
friends and colleagues.
Earn points from views and
referrals who sign up.
Learn more

Facebook

Save articles to reading lists
and access them on any device