Python simple debugging beyond print() with Icecream
Some could argue that simply using print() statements to trace the code is not really debugging. However, sometimes that’s all you need.
Let’s start with the simple example below:
> 3 5
Now let’s include the variable names to improve debugging.
> a: 3, b: 5
What if we don’t want to worry about formatting the output? Let’s use the Icecream library (pip install icecream
) to help us.
> ic| a: 3, b: 5
The statement in line 5 of the above code also prints the values of the variables. It is somehow equivalent to the previous code, but it automatically includes a prefix to print the variable name aswell.
Let’s see another example to demonstrate other nice features of Icecream.
> ic| debug_part4.py:5 in is_equal() at 10:00:20.503
> ic| is_equal(2, 2): True
The statements in lines 5 and 8 are useful to inspect the flow of the code. When we callic()
without parameters, Icecream prints the line number and the execution scope (in this case, the function `is_equal()`).
In line 11, we pass the return of the function toic()
and it prints the function call along with its parameters and return value.
Conclusion
Icecream can be very helpful if simple debug is what you need. It expands the power of simple print() without adding unnecessary complexity.
Check the docs and give it a try!