blob: 62392eb4b62be245a54ad7970a9cef1cde1d82d2 [file] [log] [blame]
Benjamin Peterson1c2353b2009-12-20 15:23:22 +00001:mod:`future_builtins` --- Python 3 builtins
2============================================
Georg Brandld3464752008-03-21 19:37:57 +00003
4.. module:: future_builtins
5.. sectionauthor:: Georg Brandl
6.. versionadded:: 2.6
7
8This module provides functions that exist in 2.x, but have different behavior in
Georg Brandld7d4fd72009-07-26 14:37:28 +00009Python 3, so they cannot be put into the 2.x builtins namespace.
Georg Brandld3464752008-03-21 19:37:57 +000010
Benjamin Peterson1c2353b2009-12-20 15:23:22 +000011Instead, if you want to write code compatible with Python 3 builtins, import
Georg Brandld3464752008-03-21 19:37:57 +000012them 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 Brandl5a42ca62008-05-20 07:20:12 +000018The :term:`2to3` tool that ports Python 2 code to Python 3 will recognize
Benjamin Peterson1c2353b2009-12-20 15:23:22 +000019this usage and leave the new builtins alone.
Georg Brandld3464752008-03-21 19:37:57 +000020
21.. note::
22
Benjamin Peterson1c2353b2009-12-20 15:23:22 +000023 The Python 3 :func:`print` function is already in the builtins, but cannot be
Georg Brandld3464752008-03-21 19:37:57 +000024 accessed from Python 2 code unless you use the appropriate future statement::
25
26 from __future__ import print_function
27
28
Benjamin Peterson1c2353b2009-12-20 15:23:22 +000029Available builtins are:
Georg Brandld3464752008-03-21 19:37:57 +000030
Georg Brandl89f48872008-06-11 18:55:38 +000031.. 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 Brandld3464752008-03-21 19:37:57 +000039.. function:: filter(function, iterable)
40
41 Works like :func:`itertools.ifilter`.
42
43.. function:: hex(object)
44
Georg Brandld7d4fd72009-07-26 14:37:28 +000045 Works like the built-in :func:`hex`, but instead of :meth:`__hex__` it will
Georg Brandld3464752008-03-21 19:37:57 +000046 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 Peterson84f323e2014-03-09 14:01:09 -050053 .. note::
54
55 In Python 3, :func:`map` does not accept ``None`` for the
56 function argument.
57
Georg Brandld3464752008-03-21 19:37:57 +000058.. function:: oct(object)
59
Georg Brandld7d4fd72009-07-26 14:37:28 +000060 Works like the built-in :func:`oct`, but instead of :meth:`__oct__` it will
Georg Brandld3464752008-03-21 19:37:57 +000061 use the :meth:`__index__` method on its argument to get an integer that is
R. David Murray195374e2009-04-04 06:39:56 +000062 then converted to octal.
Georg Brandld3464752008-03-21 19:37:57 +000063
64.. function:: zip(*iterables)
65
66 Works like :func:`itertools.izip`.