28.4. future_builtins — Python 3 内置

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.

注意

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 ( 对象 )

返回如同 repr() . In Python 3, repr() will return printable Unicode characters unescaped, while ascii() will always backslash-escape them. Using future_builtins.ascii() 而不是 repr() in 2.6 code makes it clear that you need a pure ASCII return value.

future_builtins. filter ( function , iterable )

工作像 itertools.ifilter() .

future_builtins. hex ( 对象 )

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 , ... )

工作像 itertools.imap() .

注意

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

future_builtins. oct ( 对象 )

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 )

工作像 itertools.izip() .

上一话题

28.3. __builtin__ — 内置对象

下一话题

28.5. __main__ — 顶层脚本环境

本页