跳转到主要内容

支持Python 3和2的单源清洁支持

项目描述

future 是 Python 2 和 Python 3 之间缺失的兼容层。它允许您使用单个、干净的 Python 3.x 兼容代码库,以最小的开销同时支持 Python 2 和 Python 3。

它设计如下使用

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)
from builtins import (
         bytes, dict, int, list, object, range, str,
         ascii, chr, hex, input, next, oct, open,
         pow, round, super,
         filter, map, zip)

后面跟着主要是标准的、惯用的 Python 3 代码,然后在 Python 2.6/2.7 和 Python 3.3+ 上运行类似。

导入在 Python 3 上没有效果。在 Python 2 上,它们会覆盖相应的内置函数,这些函数在 Python 3 与 2 中的语义通常不同,以提供它们的 Python 3 语义。

标准库重组

future 通过以下 Py3 接口支持标准库重组(PEP 3108)

>>> # Top-level packages with Py3 names provided on Py2:
>>> import html.parser
>>> import queue
>>> import tkinter.dialog
>>> import xmlrpc.client
>>> # etc.
>>> # Aliases provided for extensions to existing Py2 module names:
>>> from future.standard_library import install_aliases
>>> install_aliases()
>>> from collections import Counter, OrderedDict   # backported to Py2.6
>>> from collections import UserDict, UserList, UserString
>>> import urllib.request
>>> from itertools import filterfalse, zip_longest
>>> from subprocess import getoutput, getstatusoutput

自动转换

一个名为 futurize 的包含脚本有助于将代码(无论是从 Python 2 还是 Python 3)转换为与两个平台兼容的代码。它与 python-modernize 类似,但通过使用 future 中的回滚类型和内置函数提供了更进一步的 Python 3 兼容性。

文档

见:https://python-future.org

致谢

作者::

Ed Schofield, Jordan M. Adler, 等人

赞助商::

Python Charmers: https://pythoncharmers.com

其他人::

见 docs/credits.rst 或 https://python-future.org/credits.html

许可

版权 2013-2024 Python Charmers,澳大利亚。软件在 MIT 许可下分发。见 LICENSE.txt。

项目详情


发布历史 发布通知 | RSS 源

下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

future-1.0.0.tar.gz (1.2 MB 查看哈希值)

上传时间: 源代码

构建版本

future-1.0.0-py3-none-any.whl (491.3 kB 查看哈希值)

上传时间 Python 3

由以下支持