353 lines
19 KiB
HTML
353 lines
19 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>__future__ --- Future 语句定义 — 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="gc --- 垃圾回收器接口" href="gc.html" />
|
||
<link rel="prev" title="traceback —— 打印或读取栈回溯信息" href="traceback.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/__future__.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>
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="traceback.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> —— 打印或读取栈回溯信息</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="gc.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> --- 垃圾回收器接口</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/library/__future__.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/library/__future__.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="gc.html" title="gc --- 垃圾回收器接口"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="traceback.html" title="traceback —— 打印或读取栈回溯信息"
|
||
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" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="python.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="module-__future__">
|
||
<span id="future-future-statement-definitions"></span><h1><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> --- Future 语句定义<a class="headerlink" href="#module-__future__" title="永久链接至标题">¶</a></h1>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/__future__.py">Lib/__future__.py</a></p>
|
||
<hr class="docutils" />
|
||
<p><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 是一个真正的模块,这主要有 3 个原因:</p>
|
||
<ul class="simple">
|
||
<li><p>避免混淆已有的分析 import 语句并查找 import 的模块的工具。</p></li>
|
||
<li><p>确保 <a class="reference internal" href="../reference/simple_stmts.html#future"><span class="std std-ref">future 语句</span></a> 在 2.1 之前的版本运行时至少能抛出 runtime 异常(import <a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 会失败,因为 2.1 版本之前没有这个模块)。</p></li>
|
||
<li><p>当引入不兼容的修改时,可以记录其引入的时间以及强制使用的时间。这是一种可执行的文档,并且可以通过 import <a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 来做程序性的检查。</p></li>
|
||
</ul>
|
||
<p><code class="file docutils literal notranslate"><span class="pre">__future__.py</span></code> 中的每一条语句都是以下格式的:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">FeatureName</span> <span class="o">=</span> <span class="n">_Feature</span><span class="p">(</span><span class="n">OptionalRelease</span><span class="p">,</span> <span class="n">MandatoryRelease</span><span class="p">,</span>
|
||
<span class="n">CompilerFlag</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>通常 <em>OptionalRelease</em> 要比 <em>MandatoryRelease</em> 小,并且都是和 <a class="reference internal" href="sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> 格式一致的 5 元素元组。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">PY_MAJOR_VERSION</span><span class="p">,</span> <span class="c1"># the 2 in 2.1.0a3; an int</span>
|
||
<span class="n">PY_MINOR_VERSION</span><span class="p">,</span> <span class="c1"># the 1; an int</span>
|
||
<span class="n">PY_MICRO_VERSION</span><span class="p">,</span> <span class="c1"># the 0; an int</span>
|
||
<span class="n">PY_RELEASE_LEVEL</span><span class="p">,</span> <span class="c1"># "alpha", "beta", "candidate" or "final"; string</span>
|
||
<span class="n">PY_RELEASE_SERIAL</span> <span class="c1"># the 3; an int</span>
|
||
<span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><em>OptionalRelease</em> 记录了一个特性首次发布时的 Python 版本。</p>
|
||
<p>在 <em>MandatoryRelases</em> 还没有发布时,<em>MandatoryRelease</em> 表示该特性会变成语言的一部分的预测时间。</p>
|
||
<p>其他情况下,<em>MandatoryRelease</em> 用来记录这个特性是何时成为语言的一部分的。从该版本往后,使用该特性将不需要 future 语句,不过很多人还是会加上对应的 import。</p>
|
||
<p><em>MandatoryRelease</em> 也可能是 <code class="docutils literal notranslate"><span class="pre">None</span></code>, 表示这个特性已经被撤销。</p>
|
||
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code> 类的实例有两个对应的方法,<code class="xref py py-meth docutils literal notranslate"><span class="pre">getOptionalRelease()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">getMandatoryRelease()</span></code>。</p>
|
||
<p><em>CompilerFlag</em> 是一个(位)标记,对于动态编译的代码,需要将这个标记作为第四个参数传入内建函数 <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 中以开启对应的特性。这个标记存储在 <code class="xref py py-class docutils literal notranslate"><span class="pre">_Feature</span></code> 类实例的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">compiler_flag</span></code> 属性中。</p>
|
||
<p><a class="reference internal" href="#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 中不会删除特性的描述。从 Python 2.1 中首次加入以来,通过这种方式引入了以下特性:</p>
|
||
<table class="docutils align-default">
|
||
<colgroup>
|
||
<col style="width: 20%" />
|
||
<col style="width: 14%" />
|
||
<col style="width: 16%" />
|
||
<col style="width: 50%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>特性</p></th>
|
||
<th class="head"><p>可选版本</p></th>
|
||
<th class="head"><p>强制加入版本</p></th>
|
||
<th class="head"><p>效果</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p>nested_scopes</p></td>
|
||
<td><p>2.1.0b1</p></td>
|
||
<td><p>2.2</p></td>
|
||
<td><p><span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0227"><strong>PEP 227</strong></a>: <em>静态嵌套作用域</em></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>generators</p></td>
|
||
<td><p>2.2.0a1</p></td>
|
||
<td><p>2.3</p></td>
|
||
<td><p><span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0255"><strong>PEP 255</strong></a>: <em>简单生成器</em></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>division</p></td>
|
||
<td><p>2.2.0a2</p></td>
|
||
<td><p>3.0</p></td>
|
||
<td><p><span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0238"><strong>PEP 238</strong></a>: <em>修改除法运算符</em></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>absolute_import</p></td>
|
||
<td><p>2.5.0a1</p></td>
|
||
<td><p>3.0</p></td>
|
||
<td><p><span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a>: <em>导入:多行与绝对/相对</em></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>with_statement</p></td>
|
||
<td><p>2.5.0a1</p></td>
|
||
<td><p>2.6</p></td>
|
||
<td><p><span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0343"><strong>PEP 343</strong></a>: * "with" 语句*</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>print_function</p></td>
|
||
<td><p>2.6.0a2</p></td>
|
||
<td><p>3.0</p></td>
|
||
<td><p><span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a>: <em>print 改为函数</em></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>unicode_literals</p></td>
|
||
<td><p>2.6.0a2</p></td>
|
||
<td><p>3.0</p></td>
|
||
<td><p><span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3112"><strong>PEP 3112</strong></a>: <em>Python 3000 中的字节字面值</em></p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>generator_stop</p></td>
|
||
<td><p>3.5.0b1</p></td>
|
||
<td><p>3.7</p></td>
|
||
<td><p><span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0479"><strong>PEP 479</strong></a>: <em>在生成器中处理 StopIteration</em></p></td>
|
||
</tr>
|
||
<tr class="row-even"><td><p>annotations</p></td>
|
||
<td><p>3.7.0b1</p></td>
|
||
<td><p>3.10</p></td>
|
||
<td><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0563"><strong>PEP 563</strong></a>:
|
||
<em>Postponed evaluation of annotations</em></p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<dl class="simple">
|
||
<dt><a class="reference internal" href="../reference/simple_stmts.html#future"><span class="std std-ref">future 语句</span></a></dt><dd><p>编译器怎样处理 future import。</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="traceback.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code> —— 打印或读取栈回溯信息</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="gc.html"
|
||
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> --- 垃圾回收器接口</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/library/__future__.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="gc.html" title="gc --- 垃圾回收器接口"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="traceback.html" title="traceback —— 打印或读取栈回溯信息"
|
||
>上一页</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="nav-item nav-item-2"><a href="python.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> |