Python Debugging Like a Pro: Ditch print() and Use ic() Instead! 🚀

Stop spamming print(). Learn how ic() from IceCream module makes debugging faster and more insightful!

Python Debugging Like a Pro: Ditch print() and Use ic() Instead! 🚀
Photo by Tim Foster on Unsplash

Debug smarter, not harder!

Python Debugging Like a Pro: Ditch print() and Use ic() Instead! 🚀

If you’re still relying on print() for debugging, you’re missing out on a much faster, clearer, and more efficient way to troubleshoot your Python code.

Meet ic() from the icecream library—a powerful debugging tool that makes debugging fun, elegant, and incredibly useful. In this article, we’ll explore why ic() is better than print(), how to use it, and real-world scenarios where it shines!


Why ic() is Better Than print()

Using print() for debugging is:

  • Messy: Scattered print statements clutter your code.
  • Slow: You have to manually type print(variable) everywhere.
  • Uninformative: It only shows values, not where they came from.

ic() fixes all these problems by:

  • Automatically showing variable names and values
  • Displaying file name and line number
  • Being more readable and faster than print()

Installing icecream

To use ic(), first install the icecream library:

pip install icecream

Then, import it into your script:

from icecream import ic

Now, let’s see how it works!


Example 1: Automatic Variable Names and Values

With print(), debugging is manual and repetitive:

x = 42 
y = x * 2 
print("y:", y) # We have to manually type "y:"

With ic(), debugging is automatic and cleaner:

from icecream import ic 
 
x = 42 
y = x * 2 
ic(y)

Output:

ic| y: 84

No need to manually type "y:"—ic() does it for you!

Example 2: See Where Your Debugging Happens

One of the biggest issues with print() debugging is figuring out where a value was printed.

With print(), you get only the value:

print("Result:", 10 + 5) 
 
# output - Result: 15

With ic(), you also get the file name and line number:

from icecream import ic 
 
ic(10 + 5)

Output:

ic| test.py:4 in <module>: 10 + 5: 15

No more guessing where your print statement came from!

Example 3: Debugging Inside Functions

With print(), debugging inside functions is tedious:

def add(a, b): 
    print("Adding", a, "and", b) 
    return a + b 
 
print(add(5, 7))

With ic(), it’s automatic and structured:

from icecream import ic 
 
def add(a, b): 
    return ic(a + b) 
 
print(add(5, 7))

Output:

ic| test.py:4 in add(): a + b: 12 
12

You see exactly which function and line the calculation happened on!

Example 4: Debugging Loops

When debugging loops, print() gives a long, unreadable output:

for i in range(3): 
    print("i:", i, "Square:", i**2)

With ic(), everything is structured neatly:

from icecream import ic 
 
for i in range(3): 
    ic(i, i**2)

Output:

ic| test.py:4 in <module>: i: 0, i**2: 0 
ic| test.py:4 in <module>: i: 1, i**2: 1 
ic| test.py:4 in <module>: i: 2, i**2: 4

It is readable, clear, and easy to trace!

Example 5: Debugging Function Calls

Ever wondered what’s being passed into a function? ic() tracks it!

from icecream import ic 
 
def greet(name): 
    return f"Hello, {name}!" 
 
ic(greet("John")) 
ic(greet("David"))

Output:

ic| test.py:5 in <module>: greet("John"): 'Hello, Alice!' 
ic| test.py:6 in <module>: greet("David"): 'Hello, Bob!'

No need to manually print function calls — ic() does it for you!

Example 6: Temporarily Disabling ic()

Unlike print(), ic() can be disabled without removing code!

from icecream import ic 
 
ic.disable()  # Turns off debugging 
ic("This will NOT be printed!") 
 
ic.enable()   # Turns debugging back on 
ic("This WILL be printed!")

Perfect for debugging in development and turning it off in production!

Example 7: Customizing ic() Output

If you want to change how ic() displays output? Customize it like this:

from icecream import ic 
 
ic.configureOutput(prefix="DEBUG >>> ") 
ic(5 + 3)

Output:

DEBUG >>> 5 + 3: 8

Final Thoughts: Ditch print(), Use ic()!

ic() is clearer, faster, and smarter than print(). It Automatically tracks variables, functions, and locations and easy to disable in production

Next time you debug in Python, ditch print() and try ic()! It’ll save you time, frustration, and effort.

Have you tried ic() before? What’s your favorite debugging trick? Drop a comment below!


Photo by Rihards Sergis on Unsplash