Benjamin Peterson | 1c2353b | 2009-12-20 15:23:22 +0000 | [diff] [blame] | 1 | :mod:`future_builtins` --- Python 3 builtins |
| 2 | ============================================ |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 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 |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 9 | Python 3, so they cannot be put into the 2.x builtins namespace. |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | 1c2353b | 2009-12-20 15:23:22 +0000 | [diff] [blame] | 11 | Instead, if you want to write code compatible with Python 3 builtins, import |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 5a42ca6 | 2008-05-20 07:20:12 +0000 | [diff] [blame] | 18 | The :term:`2to3` tool that ports Python 2 code to Python 3 will recognize |
Benjamin Peterson | 1c2353b | 2009-12-20 15:23:22 +0000 | [diff] [blame] | 19 | this usage and leave the new builtins alone. |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 20 | |
| 21 | .. note:: |
| 22 | |
Benjamin Peterson | 1c2353b | 2009-12-20 15:23:22 +0000 | [diff] [blame] | 23 | The Python 3 :func:`print` function is already in the builtins, but cannot be |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 24 | accessed from Python 2 code unless you use the appropriate future statement:: |
| 25 | |
| 26 | from __future__ import print_function |
| 27 | |
| 28 | |
Benjamin Peterson | 1c2353b | 2009-12-20 15:23:22 +0000 | [diff] [blame] | 29 | Available builtins are: |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 30 | |
Georg Brandl | 89f4887 | 2008-06-11 18:55:38 +0000 | [diff] [blame] | 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 | |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 39 | .. function:: filter(function, iterable) |
| 40 | |
| 41 | Works like :func:`itertools.ifilter`. |
| 42 | |
| 43 | .. function:: hex(object) |
| 44 | |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 45 | Works like the built-in :func:`hex`, but instead of :meth:`__hex__` it will |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 84f323e | 2014-03-09 14:01:09 -0500 | [diff] [blame] | 53 | .. note:: |
| 54 | |
| 55 | In Python 3, :func:`map` does not accept ``None`` for the |
| 56 | function argument. |
| 57 | |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 58 | .. function:: oct(object) |
| 59 | |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 60 | Works like the built-in :func:`oct`, but instead of :meth:`__oct__` it will |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 61 | use the :meth:`__index__` method on its argument to get an integer that is |
R. David Murray | 195374e | 2009-04-04 06:39:56 +0000 | [diff] [blame] | 62 | then converted to octal. |
Georg Brandl | d346475 | 2008-03-21 19:37:57 +0000 | [diff] [blame] | 63 | |
| 64 | .. function:: zip(*iterables) |
| 65 | |
| 66 | Works like :func:`itertools.izip`. |