| 1 |
:mod:`future_builtins` --- Python 3 builtins |
| 2 |
============================================ |
| 3 |
|
| 4 |
.. module:: future_builtins |
| 5 |
.. sectionauthor:: Georg Brandl |
| 6 |
.. versionadded:: 2.6 |
| 7 |
|
| 8 |
This module provides functions that exist in 2.x, but have different behavior in |
| 9 |
Python 3, so they cannot be put into the 2.x builtins namespace. |
| 10 |
|
| 11 |
Instead, if you want to write code compatible with Python 3 builtins, import |
| 12 |
them from this module, like this:: |
| 13 |
|
| 14 |
from future_builtins import map, filter |
| 15 |
|
| 16 |
... code using Python 3-style map and filter ... |
| 17 |
|
| 18 |
The :term:`2to3` tool that ports Python 2 code to Python 3 will recognize |
| 19 |
this usage and leave the new builtins alone. |
| 20 |
|
| 21 |
.. note:: |
| 22 |
|
| 23 |
The Python 3 :func:`print` function is already in the builtins, but cannot be |
| 24 |
accessed from Python 2 code unless you use the appropriate future statement:: |
| 25 |
|
| 26 |
from __future__ import print_function |
| 27 |
|
| 28 |
|
| 29 |
Available builtins are: |
| 30 |
|
| 31 |
.. function:: ascii(object) |
| 32 |
|
| 33 |
Returns the same as :func:`repr`. In Python 3, :func:`repr` will return |
| 34 |
printable Unicode characters unescaped, while :func:`ascii` will always |
| 35 |
backslash-escape them. Using :func:`future_builtins.ascii` instead of |
| 36 |
:func:`repr` in 2.6 code makes it clear that you need a pure ASCII return |
| 37 |
value. |
| 38 |
|
| 39 |
.. function:: filter(function, iterable) |
| 40 |
|
| 41 |
Works like :func:`itertools.ifilter`. |
| 42 |
|
| 43 |
.. function:: hex(object) |
| 44 |
|
| 45 |
Works like the built-in :func:`hex`, but instead of :meth:`__hex__` it will |
| 46 |
use the :meth:`__index__` method on its argument to get an integer that is |
| 47 |
then converted to hexadecimal. |
| 48 |
|
| 49 |
.. function:: map(function, iterable, ...) |
| 50 |
|
| 51 |
Works like :func:`itertools.imap`. |
| 52 |
|
| 53 |
.. note:: |
| 54 |
|
| 55 |
In Python 3, :func:`map` does not accept ``None`` for the |
| 56 |
function argument. |
| 57 |
|
| 58 |
.. function:: oct(object) |
| 59 |
|
| 60 |
Works like the built-in :func:`oct`, but instead of :meth:`__oct__` it will |
| 61 |
use the :meth:`__index__` method on its argument to get an integer that is |
| 62 |
then converted to octal. |
| 63 |
|
| 64 |
.. function:: zip(*iterables) |
| 65 |
|
| 66 |
Works like :func:`itertools.izip`. |