Debug Python like the boss

Source: Internet
Author: User

 
 
  1. def make_pie(self, ingredients): 
  2.     print '******WHAT IS GOING ON HERE******' 
  3.     print ingredients 
  4.     self.oven.preheat() 
  5.     print self.oven.temperature 

The above code is similar to debugging code? Okay, so did I. Honestly, this is not bad. Press print to run the code and see what will happen. You must flip it in the output, but you can find the desired result. At least, when you know what you want. This is usually not the case. If you know what to test, you may not need to print those outputs. Instead, you can throw some print code to the point where you think there may be a problem, and then move these statements repeatedly to find the real bug. It's just a binary search!

Thank God, there is a better way. From the C language, a type of tool called debugger has appeared in every language. Python has a default debugger. There are also some cool debuggers developed by the community. In the following content, I will talk about some popular debugging tools.

Pdb

First, the built-in Debugger in Python, pdb. It uses a simple command line interface and has many features you can use when using the debugger. The help system can help you identify the commands you can run, such as one-step code debugging, manipulating the call stack and setting breakpoints.

Some of its missing features: tab-based Automatic population of local variables and better color and layout of code and stack tracing.

No matter which debugger you finally decide to use, pdb is worth your time to understand it for two reasons: 1) It is always available in Python 2) many other debugger functions are the superset of pdb. Learning pdb allows you to learn other debuggers more quickly.

In short, if there is better, use another debugger.

Pydbgr

Next, pydbgr supports Python 3.2 +. This project is a rewrite of the pydb debugger. It provides a group of commands very similar to pdb. However, some useful aliases such as 'U' indicate that 'up' is not available.

This debugger provides fine-grained control over execution flows. You can enter a command and specify the number of times the command is repeated. You can also tell the debugger to stop when some events are triggered, such as function calls and responses, which can reduce the number of debugging steps.

Another feature of the debugger is that it provides functions not available in other debuggers. It can perform very low-level symbol checks through commands such as examine and disassemble. To be honest, I'm not sure this feature is proven useful in web development, but it can make your work very different.

In short: I think it is not very useful for web development for the features added by pdb.

Pudb

The most obvious highlight of this debugger is that it integrates a mini GUI in the terminal. This is correct. This is not a separate window, which is in the terminal. Except for a set of default colors that can be customized or selected for a set of default themes), this is a novel attempt in debugger interaction.

The panel on the left shows the code of the current file or other files that you move up and down in the stack ). The navigation bar is great, including the arrow keys and 'hjkl 'for people who love vim), searching, and opening other modules with fuzzy queries. You can combine these simple behaviors with a shortcut key to execute code at the cursor position, and debug the program in one step in a very intuitive and friendly way. Of course, pudb still provides normal breakpoints and single-step commands so that you can control code execution as you wish.

On the right side, there are several panels to track the current local variables, stacks, and existing breakpoints. This is very different from the typical terminal debugger when you type locals (), whereh, or use a tab to reflect the current framework,

Another advantage of this debugger is that it supports post-event analysis. When you hit an exception, the debugger will use a very fast key to prompt you for this exception. It also allows you to directly jump to the line where an exception occurs, while keeping the stack and variables intact.

Conclusion: it has the best user experience in the debugger and is particularly useful for Stack tracing.

Ipdb

The last one is ipdb in iPython. Like its * pdb brothers, its interface is a simple command line and its help system is worth further research. What makes this product different is that it uses an iPython shell, so all the tabs and object reflection functions can help you find out what happened. Combining these with great syntax highlighting and a where command with the best formatting, it forms a perfect tool to eliminate bugs.

Summary: This is one of the best and my favorite.

I will mention that, depending on the situation, I switch to pudb. It is indeed the best in browsing how code runs, for example, when you have to delve into a third-party library that is not faulty ). Why do I prefer ipdb most? Most of the time, I am familiar with the code and don't need a big picture. I need to know the status in a specified function. Combine them into a super NB!

Http://blog.jobbole.com/52171/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.