356 lines
31 KiB
HTML
356 lines
31 KiB
HTML
|
|
<!DOCTYPE html>
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
|
<head>
|
|
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
|
|
|
<title>4. 执行模型 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|
<script src="../_static/jquery.js"></script>
|
|
<script src="../_static/underscore.js"></script>
|
|
<script src="../_static/doctools.js"></script>
|
|
<script src="../_static/language_data.js"></script>
|
|
<script src="../_static/translations.js"></script>
|
|
|
|
<script src="../_static/sidebar.js"></script>
|
|
|
|
<link rel="search" type="application/opensearchdescription+xml"
|
|
title="在 Python 3.8.20 文档 中搜索"
|
|
href="../_static/opensearch.xml"/>
|
|
<link rel="author" title="关于这些文档" href="../about.html" />
|
|
<link rel="index" title="索引" href="../genindex.html" />
|
|
<link rel="search" title="搜索" href="../search.html" />
|
|
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
|
<link rel="next" title="5. 导入系统" href="import.html" />
|
|
<link rel="prev" title="3. 数据模型" href="datamodel.html" />
|
|
<link rel="canonical" href="https://docs.python.org/3/reference/executionmodel.html" />
|
|
|
|
|
|
|
|
|
|
|
|
<style>
|
|
@media only screen {
|
|
table.full-width-table {
|
|
width: 100%;
|
|
}
|
|
}
|
|
</style>
|
|
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|
<script type="text/javascript" src="../_static/menu.js"></script>
|
|
|
|
</head>
|
|
<body>
|
|
<div class="mobile-nav">
|
|
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
|
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
|
<label for="menuToggler" class="toggler__label">
|
|
<span></span>
|
|
</label>
|
|
<nav class="nav-content" role="navigation">
|
|
<a href="https://www.python.org/" class="nav-logo">
|
|
<img src="../_static/py.svg" alt="Logo"/>
|
|
</a>
|
|
<div class="version_switcher_placeholder"></div>
|
|
<form role="search" class="search" action="../search.html" method="get">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
|
<path fill-rule="nonzero"
|
|
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
|
|
</svg>
|
|
<input type="text" name="q" aria-label="快速搜索"/>
|
|
<input type="submit" value="转向"/>
|
|
</form>
|
|
</nav>
|
|
<div class="menu-wrapper">
|
|
<nav class="menu" role="navigation" aria-label="main navigation">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<h3><a href="../contents.html">目录</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">4. 执行模型</a><ul>
|
|
<li><a class="reference internal" href="#structure-of-a-program">4.1. 程序的结构</a></li>
|
|
<li><a class="reference internal" href="#naming-and-binding">4.2. 命名与绑定</a><ul>
|
|
<li><a class="reference internal" href="#binding-of-names">4.2.1. 名称的绑定</a></li>
|
|
<li><a class="reference internal" href="#resolution-of-names">4.2.2. 名称的解析</a></li>
|
|
<li><a class="reference internal" href="#builtins-and-restricted-execution">4.2.3. 内置命名空间和受限的执行</a></li>
|
|
<li><a class="reference internal" href="#interaction-with-dynamic-features">4.2.4. 与动态特性的交互</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#exceptions">4.3. 异常</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="datamodel.html"
|
|
title="上一章"><span class="section-number">3. </span>数据模型</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="import.html"
|
|
title="下一章"><span class="section-number">5. </span>导入系统</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>本页</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">报告 Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.8/Doc/reference/executionmodel.rst"
|
|
rel="nofollow">显示源代码
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
|
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
|
你应当升级版本,并阅读
|
|
<a href="/3/reference/executionmodel.html"> Python 当前稳定版本的文档</a>.
|
|
</div>
|
|
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>导航</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="总目录"
|
|
accesskey="I">索引</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python 模块索引"
|
|
>模块</a> |</li>
|
|
<li class="right" >
|
|
<a href="import.html" title="5. 导入系统"
|
|
accesskey="N">下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="datamodel.html" title="3. 数据模型"
|
|
accesskey="P">上一页</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.8.20 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python语言参考</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
|
<input type="submit" value="转向" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<section id="execution-model">
|
|
<span id="execmodel"></span><h1><span class="section-number">4. </span>执行模型<a class="headerlink" href="#execution-model" title="永久链接至标题">¶</a></h1>
|
|
<section id="structure-of-a-program">
|
|
<span id="prog-structure"></span><span id="index-0"></span><h2><span class="section-number">4.1. </span>程序的结构<a class="headerlink" href="#structure-of-a-program" title="永久链接至标题">¶</a></h2>
|
|
<p id="index-1">Python 程序是由代码块构成的。 <em class="dfn">代码块</em> 是被作为一个单元来执行的一段 Python 程序文本。 以下几个都是代码块:模块、函数体和类定义。 交互式输入的每条命令都是一个代码块。 一个脚本文件(作为标准输入发送给解释器或是作为命令行参数发送给解释器的文件)也是一个代码块。 一条脚本命令(通过 <a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> 选项在解释器命令行中指定的命令)也是一个代码块。 传递给内置函数 <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 的字符串参数也是代码块。</p>
|
|
<p id="index-2">代码块在 <em class="dfn">执行帧</em> 中被执行。 一个帧会包含某些管理信息(用于调试)并决定代码块执行完成后应前往何处以及如何继续执行。</p>
|
|
</section>
|
|
<section id="naming-and-binding">
|
|
<span id="naming"></span><h2><span class="section-number">4.2. </span>命名与绑定<a class="headerlink" href="#naming-and-binding" title="永久链接至标题">¶</a></h2>
|
|
<section id="binding-of-names">
|
|
<span id="bind-names"></span><span id="index-3"></span><h3><span class="section-number">4.2.1. </span>名称的绑定<a class="headerlink" href="#binding-of-names" title="永久链接至标题">¶</a></h3>
|
|
<p id="index-4"><em class="dfn">名称</em> 用于指代对象。 名称是通过名称绑定操作来引入的。</p>
|
|
<p id="index-5">以下构造会绑定名称:传给函数的正式形参,<a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> 语句,类与函数定义(这会在定义的代码块中绑定类或函数名称)以及发生以标识符为目标的赋值,<a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> 循环的开头,或 <a class="reference internal" href="compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句和 <a class="reference internal" href="compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 子句的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> 之后。 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code> 语句的 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">...</span> <span class="pre">import</span> <span class="pre">*</span></code> 形式会绑定在被导入模块中定义的所有名称,那些以下划线开头的除外。 这种形式仅在模块层级上被使用。</p>
|
|
<p><a class="reference internal" href="simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> 语句的目标也被视作一种绑定(虽然其实际语义为解除名称绑定)。</p>
|
|
<p>每条赋值或导入语句均发生于类或函数内部定义的代码块中,或是发生于模块层级(即最高层级的代码块)。</p>
|
|
<p id="index-6">如果名称绑定在一个代码块中,则为该代码块的局部变量,除非声明为 <a class="reference internal" href="simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 或 <a class="reference internal" href="simple_stmts.html#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a>。 如果名称绑定在模块层级,则为全局变量。 (模块代码块的变量既为局部变量又为全局变量。) 如果变量在一个代码块中被使用但不是在其中定义,则为 <em class="dfn">自由变量</em>。</p>
|
|
<p>每个在程序文本中出现的名称是指由以下名称解析规则所建立的对该名称的 <em class="dfn">绑定</em>。</p>
|
|
</section>
|
|
<section id="resolution-of-names">
|
|
<span id="resolve-names"></span><h3><span class="section-number">4.2.2. </span>名称的解析<a class="headerlink" href="#resolution-of-names" title="永久链接至标题">¶</a></h3>
|
|
<p id="index-7"><em class="dfn">作用域</em> 定义了一个代码块中名称的可见性。 如果代码块中定义了一个局部变量,则其作用域包含该代码块。 如果定义发生于函数代码块中,则其作用域会扩展到该函数所包含的任何代码块,除非有某个被包含代码块引入了对该名称的不同绑定。</p>
|
|
<p id="index-8">当一个名称在代码块中被使用时,会由包含它的最近作用域来解析。 对一个代码块可见的所有这种作用域的集合称为该代码块的 <em class="dfn">环境</em>。</p>
|
|
<p id="index-9">当一个名称完全找不到时,将会引发 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 异常。 如果当前作用域为函数作用域,且该名称指向一个局部变量,而此变量在该名称被使用的时候尚未绑定到特定值,将会引发 <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> 异常。 <a class="reference internal" href="../library/exceptions.html#UnboundLocalError" title="UnboundLocalError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnboundLocalError</span></code></a> 为 <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> 的一个子类。</p>
|
|
<p>如果一个代码块内的任何位置发生名称绑定操作,则代码块内所有对该名称的使用会被认为是对当前代码块的引用。 当一个名称在其被绑定前就在代码块内被使用时则会导致错误。 这个一个很微妙的规则。 Python 缺少声明语法,并允许名称绑定操作发生于代码块内的任何位置。 一个代码块的局部变量可通过在整个代码块文本中扫描名称绑定操作来确定。</p>
|
|
<p>如果 <a class="reference internal" href="simple_stmts.html#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 语句出现在一个代码块中,则所有对该语句所指定名称的使用都是在最高层级命名空间内对该名称绑定的引用。 名称在最高层级命名内的解析是通过全局命名空间,也就是包含该代码块的模块的命名空间,以及内置命名空间即 <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> 模块的命名空间。 全局命名空间会先被搜索。 如果未在其中找到指定名称,再搜索内置命名空间。 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code> 语句必须位于所有对其所指定名称的使用之前。</p>
|
|
<p><a class="reference internal" href="simple_stmts.html#global"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">global</span></code></a> 语句与同一代码块中名称绑定具有相同的作用域。 如果一个自由变量的最近包含作用域中有一条 global 语句,则该自由变量也会被当作是全局变量。</p>
|
|
<p><a class="reference internal" href="simple_stmts.html#nonlocal"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">nonlocal</span></code></a> 语句会使得相应的名称指向之前在最近包含函数作用域中绑定的变量。 如果指定名称不存在于任何包含函数作用域中则将在编译时引发 <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>。</p>
|
|
<p id="index-10">模块的作用域会在模块第一次被导入时自动创建。 一个脚本的主模块总是被命名为 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>。</p>
|
|
<p>类定义代码块以及传给 <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 的参数是名称解析上下文中的特殊情况。 类定义是可能使用并定义名称的可执行语句。 这些引用遵循正常的名称解析规则,例外之处在于未绑定的局部变量将会在全局命名空间中查找。 类定义的命名空间会成为该类的属性字典。 在类代码块中定义的名称的作用域会被限制在类代码块中;它不会扩展到方法的代码块中 -- 这也包括推导式和生成器表达式,因为它们都是使用函数作用域实现的。 这意味着以下代码将会失败:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
|
<span class="n">a</span> <span class="o">=</span> <span class="mi">42</span>
|
|
<span class="n">b</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">a</span> <span class="o">+</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
|
|
</pre></div>
|
|
</div>
|
|
</section>
|
|
<section id="builtins-and-restricted-execution">
|
|
<span id="restrict-exec"></span><h3><span class="section-number">4.2.3. </span>内置命名空间和受限的执行<a class="headerlink" href="#builtins-and-restricted-execution" title="永久链接至标题">¶</a></h3>
|
|
<div class="impl-detail compound" id="index-11">
|
|
<p><strong>CPython implementation detail:</strong> 用户不应该接触 <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code>,严格说来它属于实现细节。 用户如果要重载内置命名空间中的值则应该 <a class="reference internal" href="simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> 并相应地修改该模块中的属性。</p>
|
|
</div>
|
|
<p>与一个代码块的执行相关联的内置命名空间实际上是通过在其全局命名空间中搜索名称 <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> 来找到的;这应该是一个字典或一个模块(在后一种情况下会使用该模块的字典)。 默认情况下,当在 <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> 模块中时,<code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> 就是内置模块 <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a>;当在任何其他模块中时,<code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> 则是 <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> 模块自身的字典的一个别名。</p>
|
|
</section>
|
|
<section id="interaction-with-dynamic-features">
|
|
<span id="dynamic-features"></span><h3><span class="section-number">4.2.4. </span>与动态特性的交互<a class="headerlink" href="#interaction-with-dynamic-features" title="永久链接至标题">¶</a></h3>
|
|
<p>自由变量的名称解析发生于运行时而不是编译时。 这意味着以下代码将打印出 42:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mi">10</span>
|
|
<span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
|
|
<span class="n">i</span> <span class="o">=</span> <span class="mi">42</span>
|
|
<span class="n">f</span><span class="p">()</span>
|
|
</pre></div>
|
|
</div>
|
|
<p><a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 函数没有对完整环境的访问权限来解析名称。 名称可以在调用者的局部和全局命名空间中被解析。 自由变量的解析不是在最近包含命名空间中,而是在全局命名空间中。 <a class="footnote-reference brackets" href="#id3" id="id1">1</a> <a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> 和 <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 函数有可选参数用来重载全局和局部命名空间。 如果只指定一个命名空间,则它会同时作用于两者。</p>
|
|
</section>
|
|
</section>
|
|
<section id="exceptions">
|
|
<span id="id2"></span><h2><span class="section-number">4.3. </span>异常<a class="headerlink" href="#exceptions" title="永久链接至标题">¶</a></h2>
|
|
<span class="target" id="index-12"></span><p id="index-13">异常是中断代码块的正常控制流程以便处理错误或其他异常条件的一种方式。 异常会在错误被检测到的位置 <em>引发</em>,它可以被当前包围代码块或是任何直接或间接发起调用发生错误的代码块的其他代码块所 <em>处理</em>。</p>
|
|
<p>Python 解析器会在检测到运行时错误(例如零作为被除数)的时候引发异常。 Python 程序也可以通过 <a class="reference internal" href="simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> 语句显式地引发异常。 异常处理是通过 <a class="reference internal" href="compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> ... <a class="reference internal" href="compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 语句来指定的。 该语句的 <a class="reference internal" href="compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句可被用来指定清理代码,它并不处理异常,而是无论之前的代码是否发生异常都会被执行。</p>
|
|
<p id="index-14">Python 的错误处理采用的是“终止”模型:异常处理器可以找出发生了什么问题,并在外层继续执行,但它不能修复错误的根源并重试失败的操作(除非通过从顶层重新进入出错的代码片段)。</p>
|
|
<p id="index-15">当一个异常完全未被处理时,解释器会终止程序的执行,或者返回交互模式的主循环。 无论是哪种情况,它都会打印栈回溯信息,除非是当异常为 <a class="reference internal" href="../library/exceptions.html#SystemExit" title="SystemExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SystemExit</span></code></a> 的时候。</p>
|
|
<p>异常是通过类实例来标识的。 <a class="reference internal" href="compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> 子句会依据实例的类来选择:它必须引用实例的类或是其所属的基类。 实例可通过处理器被接收,并可携带有关异常条件的附加信息。</p>
|
|
<div class="admonition note">
|
|
<p class="admonition-title">注解</p>
|
|
<p>异常消息不是 Python API 的组成部分。 其内容可能在 Python 升级到新版本时不经警告地发生改变,不应该被需要在多版本解释器中运行的代码所依赖。</p>
|
|
</div>
|
|
<p>另请参看 <a class="reference internal" href="compound_stmts.html#try"><span class="std std-ref">try 语句</span></a> 小节中对 <a class="reference internal" href="compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> 语句的描述以及 <a class="reference internal" href="simple_stmts.html#raise"><span class="std std-ref">raise 语句</span></a> 小节中对 <a class="reference internal" href="simple_stmts.html#raise"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">raise</span></code></a> 语句的描述。</p>
|
|
<p class="rubric">备注</p>
|
|
<dl class="footnote brackets">
|
|
<dt class="label" id="id3"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
|
|
<dd><p>出现这样的限制是由于通过这些操作执行的代码在模块被编译的时候并不可用。</p>
|
|
</dd>
|
|
</dl>
|
|
</section>
|
|
</section>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="../contents.html">目录</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">4. 执行模型</a><ul>
|
|
<li><a class="reference internal" href="#structure-of-a-program">4.1. 程序的结构</a></li>
|
|
<li><a class="reference internal" href="#naming-and-binding">4.2. 命名与绑定</a><ul>
|
|
<li><a class="reference internal" href="#binding-of-names">4.2.1. 名称的绑定</a></li>
|
|
<li><a class="reference internal" href="#resolution-of-names">4.2.2. 名称的解析</a></li>
|
|
<li><a class="reference internal" href="#builtins-and-restricted-execution">4.2.3. 内置命名空间和受限的执行</a></li>
|
|
<li><a class="reference internal" href="#interaction-with-dynamic-features">4.2.4. 与动态特性的交互</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#exceptions">4.3. 异常</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="datamodel.html"
|
|
title="上一章"><span class="section-number">3. </span>数据模型</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="import.html"
|
|
title="下一章"><span class="section-number">5. </span>导入系统</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>本页</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../bugs.html">报告 Bug</a></li>
|
|
<li>
|
|
<a href="https://github.com/python/cpython/blob/3.8/Doc/reference/executionmodel.rst"
|
|
rel="nofollow">显示源代码
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related" role="navigation" aria-label="related navigation">
|
|
<h3>导航</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="总目录"
|
|
>索引</a></li>
|
|
<li class="right" >
|
|
<a href="../py-modindex.html" title="Python 模块索引"
|
|
>模块</a> |</li>
|
|
<li class="right" >
|
|
<a href="import.html" title="5. 导入系统"
|
|
>下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="datamodel.html" title="3. 数据模型"
|
|
>上一页</a> |</li>
|
|
|
|
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|
<li class="switchers">
|
|
<div class="language_switcher_placeholder"></div>
|
|
<div class="version_switcher_placeholder"></div>
|
|
</li>
|
|
<li>
|
|
|
|
</li>
|
|
<li id="cpython-language-and-version">
|
|
<a href="../index.html">3.8.20 Documentation</a> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >Python语言参考</a> »</li>
|
|
<li class="right">
|
|
|
|
|
|
<div class="inline-search" role="search">
|
|
<form class="inline-search" action="../search.html" method="get">
|
|
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
|
<input type="submit" value="转向" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
|
<br />
|
|
This page is licensed under the Python Software Foundation License Version 2.
|
|
<br />
|
|
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
|
<br />
|
|
|
|
<br />
|
|
|
|
The Python Software Foundation is a non-profit corporation.
|
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|
<br />
|
|
<br />
|
|
|
|
最后更新于 12月 09, 2024.
|
|
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
|
<br />
|
|
|
|
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
|
</div>
|
|
|
|
</body>
|
|
</html> |