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

What are the ways to debug code in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what are the ways to debug code in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the ways to debug code in Python"?

Print

The print function is easy to use, so we often use it for simple debugging to print whether a value is expected. However, print also has some shortcomings. If you want to print a complex object, or a long dictionary, list and other composite objects, the printed results are stacked on one line, which is very unfriendly, and it is not easy to see the structure of the data, for example:

Pprint

Fortunately, Python has built-in another printing tool, pprint, whose full name is pretty printer. As the name implies, it is a printing tool with beauty effect. When printing complex objects, it is output in a readable format. Of course, you can also specify the maximum width of indents and lines, which is very helpful when you encounter complex objects, such as:

Prettyprinter

There is no end to the human pursuit of beauty, although pprint is enough, but some people always think that it is not beautiful enough, so someone has come up with a more powerful printing tool called prettyprinter, which is a third-party tool that needs additional installation. You can see that the time object is more readable, but I feel a little redundant and can change color in Linux.

Logging

It is convenient and easy to debug code with print in the development process, but be sure to get rid of these redundant code before going online, otherwise it will also affect performance. Then what to do if you want to track some key locations online, then logging will play its role. In many cases, exceptions that will not occur in the test environment will occur in the formal environment, because of the various critical conditions you have to face, the benefits of using logging are self-evident. You can set different log levels, such as debug, info, error, etc., and you can persist the log to facilitate subsequent log tracking.

PDB

PBD is Python's built-in module, is a real code debugging, because you can set breakpoints while the code is running, step into the function debugging, view the current code, view stack fragments, dynamically change the value of variables, and so on. When PDB was not integrated with the development tool IDE in the early days, it was very useful (and is still in use now), requiring only one command:

Python-m pdb test.py

You can enter the debugging module, students who do not like to use IDE can consider using it.

PyCharm

But now there are more advanced development tools, such as PyCharm, PyDev and other tools support code debugging, breakpoints, dynamic modification of variable values, and so on, not only that, PyCharm also supports remote debugging. In the case of Bug hiding more deeply, we sometimes sacrifice such a mass killer.

Thank you for reading, the above is the content of "what are the ways to debug code in Python?" after the study of this article, I believe you have a deeper understanding of the way of debugging code in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report