It is convenient to use Icecream instead of print when debugging.

I read this article and found it very useful, so I would like to share it.

motivation

Let's assume the following code.

num1 = 30
num2 = 40

print(num1)
print(num2)

The output is

30
40

It will be. Now, by looking at the output above, can you quickly tell whether 30 or 40 corresponds to num1 or num2? As long as there are few output variables, this does not seem to be a problem, but when it comes to 5, it becomes a little troublesome.

For the time being, the solution is as follows.

num1 = 30
num2 = 40

print('num1', num1)
print('num2', num2)

The output is

num1 30
num2 40

Certainly, the correspondence between variable names and variable values ​​is easier to understand than in the original code. However, the problem is that writing the code is cumbersome.

What is Icecream?

The solution to the above problem is the python library Icecream. Installation can be done with pip.

pip install icecream
from icecream import ic

num1 = 30
num2 = 40

ic(num1)
ic(num2)

The output is

ic| num1: 30
ic| num2: 40

Very simple and nice! This is not the only convenience of Icecream. You can use functions and conditional branching. If you are interested, please check here.

Recommended Posts

It is convenient to use Icecream instead of print when debugging.
It is convenient to use Layers when putting a library on Lambda
It is more convenient to use csv-table when writing a table with python-sphinx
It is convenient to use Icecream instead of print when debugging.
When I try to use pip, SSL module is not available.
Is it deprecated to use pip directly?
When you want to use it as it is when using it with lambda memo
It is better to use Weight Initializer when initializing with HeNormal with Chainer
It is convenient to use stac_info and exc_info when you want to display traceback in log output by logging.
Convenient use of ipython
Memo of how to use properly when combining pandas.DataFrame
I realized that it is nonsense to use the module without thinking because it is convenient.
[C language] Close () It is dangerous to retry when it fails
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
When it is troublesome to copy what you built with vue
Comparison of GCP computing services [I want to use it serverless]
Let's use usercustomize.py instead of sitecustomize.py
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
How to add sudo when debugging
[Python Queue] Convenient use of Deque
Summary of how to use csvkit
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
Use edump instead of var_dump for easy debugging & efficient data content (PHP)
Use Bucket (). Objects.filter instead of list_objects_v2 when outputting S3 list with Boto3
[Python] It was very convenient to use a Python class for a ROS program.
Why you should use urlopen instead of urlretrieve to download Python files