📄 index.lxp@lxpwrap=x993_252ehtm.htm
字号:
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>Debugging Techniques</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Debugging"HREF="c883.htm"><LINKREL="PREVIOUS"TITLE="Stepping along"HREF="x966.htm"><LINKREL="NEXT"TITLE="If all else fails"HREF="x1024.htm"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x966_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 5. Debugging</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x1024_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1">Debugging Techniques</A></H1><P>Once you know your debugger, the real work can begin. Here's a short overview of debugging techniques that I have found useful.</P><DIVCLASS="SECT2"><H2CLASS="SECT2">Avoid changing your code</A></H2><P>The temptation to change the code is almost too strong to resist. Perhaps your application doesn't work, and you don't have a solid idea of where the problem is, but only a hunch. So you start changing code. Suddenly you notice another problem—and so you change more code. Nothing works, so you change some more code...</P><P>Eventually, the original bug disappears — or the application simply crashes before it reaches the bug!</P><P>So, the golden rule is: ‘Don't change code at random'. Indeed don't change any code at all before you know exactly where the problem is. (I'm not talking about a sprinkling of <TTCLASS="FUNCTION">print</TT> statements here, of course.)</P><P>There's another point to the maxim ‘don't change your code': you should debug problems <SPAN><ICLASS="EMPHASIS">now</I></SPAN>, and not when you've finished writing the application. If you don't fix a bug as soon as you come across it, you'll probably never fix it. And bugs don't disappear because you added more code.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Gather data</A></H2><P>Try to determine the pattern of failures. This pattern might be caused by data or by usage patterns. That's why it is so important to have other people test your software (a cat walking on a keyboard is an excellent input simulator!). You know all the ‘right' paths through your code, so you'll probably never bring it down. Experienced users know those paths, too, which is why software appears to become more stable after people have been using it for a longer time.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2">Minimal examples</A></H2><P>Sometimes you simply want to know whether a certain Python construction works at all. Nobody is perfect, and everyone has little things they are perpetually unsure about. Mine is string slicing. I never know what exactly will be returned by <TTCLASS="FUNCTION">"bla.bla"[:-4]</TT>.</P><P>In chapter five of the excellent book <ICLASS="CITETITLE">The Practice of Programming</I>, Kernighan and Pike advise to try out your hunches with a little bit of code, whenever you wonder whether something works at all. The Python shell window is tailor made for this:</P><DIVCLASS="MEDIAOBJECT"><P><DIVCLASS="CAPTION"><P>I wonder what happens when...</P></DIV></P></DIV></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><A accesskey="P" href="index.lxp@lxpwrap=x966_252ehtm.htm">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="H" href="index.lxp@lxpwrap=book1_252ehtm">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><A accesskey="N" href="index.lxp@lxpwrap=x1024_252ehtm.htm">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Stepping along</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><A accesskey="U" href="index.lxp@lxpwrap=c883_252ehtm.htm">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">If all else fails</TD></TR></TABLE></DIV></BODY></HTML> </td> </tr> </table> </td> </tr> </table>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -