⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 60.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -&gt; Assertions and _ _debug_ _ </TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="54.html" class="navtitle">5. Control Flow</a> &gt; <span class="nonavtitle">Assertions and _ _debug_ _ </span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="59.html" title="Defining New Exceptions"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=60" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="60.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="62.html" title="6. Functions and Functional Programming"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A58%3A16+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104016132042164068215009</font><a href="read5.asp?bookname=0735710910&snode=60&now=5%2F28%2F2002+8%3A58%3A16+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Assertions and <tt cLASS="monofont">_ _debug_ _</tt></h3>
<p>The <TT CLass="monofont">assert</tT>  statement is used to add debugging code into a program. The general form of <TT Class="monofont">assert</tt>  is</p>

<pre>

assert <i>test</i> [, <i>data</i>] </pre>

<p>where <i><tT clAss="monofont">test</tT></i>
 is an expression that should evaluate to true or false. If <i><tt ClasS="monofont">test</TT></I>
 evaluates to false, <tt clASS="monofont">assert</Tt>  raises an <tt cLASS="monofont">AssertionError</tt>  exception with the optional <i><tT CLAss="monofont">data</tt></i>
 supplied to the <tt class="monofont">assert</tt>  statement. For example:</p>

<pre>

def write_data(file,data): 
    assert file, "write_data: file is None!" 
    ... </prE>

<p>Internally, the <tT claSs="monofont">assert</tt>  statement is translated into the following code:</p>

<Pre>

if _ _debug_ _: 
   if not (<i>test</I>): 
      raise AssertionError, <I>data</I> </Pre>

<p><tT CLAss="monofont">_ _debug_ _</tt> is a built-in read-only value that抯 set to <TT CLass="monofont">1</tT> unless the interpreter is running in optimized mode (specified with the <TT Class="monofont">-O</tt>  option). Although <tt class="monofont">_ _debug_ _</tt> is used by assertions, you also can use it to include any sort of debugging code.</p>

<p>The <tt cLasS="monofont">assert</tt>  statement should not be used for code that must be executed to make the program correct, since it won抰 be executed if Python is run in optimized mode. In particular, it抯 an error to use <tT clasS="monofont">assert</tt>  to check user input. Instead, <tT CLAss="monofont">assert</tt>  statements are used to check things that should always be true; if one is violated, it represents a bug in the program, not an error by the user.</P>

<P>For example, if the function <TT clasS="monofont">write_data()</TT> shown here were intended for use by an end user, the <Tt claSS="monofont">assert</TT>  statement should be replaced by a conventional <tt class="monofont">if</tt> statement and the desired error handling.</p>
</font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, &copy;&nbsp;2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="59.html" title="Defining New Exceptions"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=60" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="60.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="62.html" title="6. Functions and Functional Programming"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
</TD></TR></TABLE>




<!--EndOfBrowse-->

</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -