Simplify Debugging in Python with Icecream ??
Shailesh Mishra
Linkedin Top Voice (awarded by Linkedin) | AWS, Ex (Google, Oracle, IBM, TCS) | Public Speaking | Writer, Author | Tech Leader | Mentor (Empowering the Next Generation) | Trusted Advisor to Fortune 500 Companies
?? Are you still debugging with print() statements? There's a better way—meet the icecream library! ??
The icecream library is a lightweight tool that makes debugging easier by showing you variable names, values, and where the debugging statement is located—all in a clean and readable format. Let’s compare:
Example 1: Traditional print() vs Icecream ic()
# Using print()
def example1(a, b):
print("a =", a, "b =", b)
return a + b
example1(3, 5)
Output: a = 3 b = 5
# Using icecream
from icecream import ic
def example2(a, b):
ic(a, b)
return a + b
example2(3, 5)
Output: ic| a: 3, b: 5
Example 2: Debugging Function Behavior
from icecream import ic
def factorial(n):
if n == 0:
return 1
ic(n)
return n * factorial(n - 1)
factorial(4)
Output:
ic| n: 4
ic| n: 3
ic| n: 2
ic| n: 1
Example 3: Debugging Objects and Expressions
from icecream import ic
my_list = [1, 2, 3]
ic(my_list, len(my_list), sum(my_list))
Output: ic| my_list: [1, 2, 3], len(my_list): 3, sum(my_list): 6
Why Icecream?
?? Installation:
pip install icecream
Have you tried icecream yet? Share your thoughts or debugging tips in the comments! ??
#Python #Debugging #IcecreamLibrary #CodeTips #Programming