28.4. future_builtins — Python 3 builtins - Python 2.7.18 documentation 编辑

New in version 2.6.

This module provides functions that exist in 2.x, but have different behavior in Python 3, so they cannot be put into the 2.x builtins namespace.

Instead, if you want to write code compatible with Python 3 builtins, import them from this module, like this:

from future_builtins import map, filter

... code using Python 3-style map and filter ...

The 2to3 tool that ports Python 2 code to Python 3 will recognize this usage and leave the new builtins alone.

Note

The Python 3 print() function is already in the builtins, but cannot be accessed from Python 2 code unless you use the appropriate future statement:

from __future__ import print_function

Available builtins are:

future_builtins.ascii(object)

Returns the same as repr(). In Python 3, repr() will return printable Unicode characters unescaped, while ascii() will always backslash-escape them. Using future_builtins.ascii() instead of repr() in 2.6 code makes it clear that you need a pure ASCII return value.

future_builtins.filter(function, iterable)

Works like itertools.ifilter().

future_builtins.hex(object)

Works like the built-in hex(), but instead of __hex__() it will use the __index__() method on its argument to get an integer that is then converted to hexadecimal.

future_builtins.map(function, iterable, ...)

Works like itertools.imap().

Note

In Python 3, map() does not accept None for the function argument.

future_builtins.oct(object)

Works like the built-in oct(), but instead of __oct__() it will use the __index__() method on its argument to get an integer that is then converted to octal.

future_builtins.zip(*iterables)

Works like itertools.izip().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:57 次

字数:3339

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文