Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not use Print to debug Python

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "Why not use Print to debug Python". In daily operation, I believe many people have doubts about why they do not use Print to debug Python. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "Why not use Print to debug Python". Next, please follow the editor to study!

I believe that most people who learn Python will definitely use the built-in function print () to debug the code.

So in a large project, if you also use print to debug your Python code, you will find that your terminal has multiple outputs.

Then you have to tell which code is running on each line of output.

For example, run the following program.

Num1 = 30 num2 = 40 print (num1) print (num2)

Output the results.

30 40

Which of these outputs is num1? Which one is num2?

It may not be difficult to find two outputs, but what if there are more than five different outputs? Trying to find code related to the output can be time-consuming.

Of course, you can add text to the printed statement to make it easier to understand:

Num1 = 30 num2 = 40 print ("num1" num1) print ("num2" num1)

Output the results.

Num1 30 num2 40

The result is easy to understand, but it takes time to write relevant information.

Then it's time for "Icecream" to play.

01. What is Icecream?

Icecream is a Python third-party library that makes print debugging clearer with minimal code.

Install the Icecream library using pip.

Pip install icecream

Next, let's try it by printing the output of the Python function.

From icecream import ic def plus_five (num): return num + 5 ic (plus_five (4)) ic (plus_five (5))

The output is as follows.

Ic | plus_five (4): 9 ic | plus_five (5): 10

By using icecream, we can see not only the function output, but also the function and its arguments!

02. Check the implementation

If you want to find out where the code is executed, you can find out which statement was executed by performing the actions shown below.

Def hello (user:bool): if user: print ("Ihumm user") else: print ("I'm not user") hello (user=True)

Output the results.

Isimm user

With icecream, you can easily do this without extra text information.

From icecream import ic def hello (user:bool): if user: ic () else: ic () hello (user=True)

The output is as follows.

Ic | ice_1.py:5 in hello () at 02MU 34MU 41.391

From the output, line 5 of the function hello has been executed, while line 7 has not.

03. Custom prefix

Icecream is also possible if you want to insert a custom prefix (such as code execution time) in a print statement.

From datetime import datetime from icecream import ic import time from datetime import datetime def time_format (): return f'{datetime.now ()} | > 'ic.configureOutput (prefix=time_format) for _ in range (3): time.sleep (1) ic (' Hello')

The output is as follows.

2021-01-24 10 Hello' 38 Hello' 23.509304 | > 'Hello' 2021-01-24 10 Hello' 38 38 Hello' 24.545628 | >' VLV 2021-01-24 10 purse 38 25 550777 | >

You can see the execution time of the code, which is displayed just before the output.

04. Get more information

In addition to knowing the code related to the output, you may also want to know the lines and code files that the code executes.

In ic.configureOutput (), set the parameter value of includeecontext to True.

From icecream import ic def plus_five (num): return num + 5 ic.configureOutput (includeContext=True) ic (plus_five (4)) ic (plus_five (5))

The output is as follows.

Ic | ice_test.py:7 in-plus_five (4): 9 ic | ice_test.py:8 in-plus_five (5): 10

Here we know that the first output is executed by the function plus_five on line 7 of the file icecream_example.py.

The second output is executed by the function plus_five on line 8 of the code file.

Both of the above operations use the ic.configureOutput () function.

By looking at the source code, you can see that there are four parameters that can be set.

Prefix, custom output prefix

OutputFunction, change the output function

ArgToStringFunction, custom parameter serialization string

IncludeContext, which displays file name, line of code, and function information

05. Delete Icecream code

Finally, you can use icecream only for debugging and print for other purposes (such as beautiful printing).

From icecream import ic def plus_five (num): return num + 5 ic.configureOutput (includeContext=True) ic (plus_five (4)) ic (plus_five (5)) for i in range (10): print (flip * Training model {I} *')

Output the results.

Ic | ice_1.py:7 in-plus_five (4): 9 ic | ice_1.py:8 in-plus_five (5): 10 * Training model 0 * * Training model 1 * * Training model 2 * * Training model 3 * * Training model 4 * * * * Training model 5 * * Training model 6 * * Training model 7 * * Training model 8 * * Training model 9 *

Because you can tell debug printing from beautiful printing, it's easy to search for and delete all ic debug statements.

After you remove all debug code, your Python code will be neat.

At this point, the study on "Why not use Print to debug Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report