[PYTHON] __future__ module and future_builtins module

In Python 2.6 or later, you can change the behavior of some functions and instructions to the behavior of Python 3 system by inserting the following code.

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import

Limited to Python 2.6 or later 2 series (2.6, 2.7), the following can also be used.

from future_builtins import ascii, filter, hex, map, oct, zip

future_builtins, unlike __future__, does not exist in Python 3 series, so When supporting both systems, it is necessary to check the version before importing, or to enclose it in try / except.

For details on each module, refer to the following documents.

The situation is the same for Python 2.7, and you can use the same code as above. Python 2.6 is included in this chapter mainly because Python 2.6 may survive (I understand) in the next few years on RHEL / CentOS 6. In conclusion, there are no significant differences in the behavior of __future__ and future_builtins between Python 2.6 and Python 2.7.

As a general rule, Python 2.6 should not be used by anyone who is allowed to choose the Python runtime, such as pyenv. If you have the freedom to choose the runtime, it's better to think about Python 2.7, that is, not using the entire Python 2 system. However, it is still possible that the library you want to use (or the Python runtime) is tied to the Python 2 system because it only supports the 2 system.

__future__ is a relatively old module introduced in Python 2.1, and has been used in older versions of Python when you want to capture some of the behavior of the new version first. Nowadays, it is often mentioned to bring the behavior of Python 2 series and 3 series closer, but it does not necessarily exist for 3 series support.

Some features of __future__ don't make sense to use in Python 2.6, and while they are harmless, they can be confusing. The explanation in future statement is as follows.

The features that Python 2.6 recognizes are unicode_literals, print_function, absolute_import, division, generators, nested_scopes, with_statement. generators, with_statement, nested_scopes are verbose as they are always valid in Python 2.6 and above.

future_builtins is a module provided to" provide a function that exists in Python 2.x but cannot be added to the Python 2.x built-in namespace because it behaves differently in Python 3. "6 Kind of function is defined. Introduced in Python 2.6. As mentioned above, it is a module that does not exist in Python 3 series, so if you try to use it in 3 series, it will be an error instead of being ignored. Coding that is conscious of both 2nd and 3rd systems seems to require some caution.

Each function imported by future_builtins is devised to behave similarly to the equivalent built-in functions of the Python 3 series, but some behave differently. You cannot assume that they behave exactly the same.

To be exact, it is different from the data type, but it is not recommended in Python to change the behavior by looking at the data type for this kind of object, so I will not consider it in the first place.

Typically, the behavior when the first argument of map () is None is different.

Python 2.7.12 (default, Jul 20 2016, 12:19:03)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from future_builtins import map
>>> list(map(None, [1, 2, 3]))
[(1,), (2,), (3,)]

The entity of future_builtins.map in Python 2.7 is said to be ʻitertools.imap (). As you can see in [Document](http://docs.python.jp/2/library/itertools.html#itertools.imap), this function has a unique behavior when the first argument function is None`. is there.

If function is None, imap () returns a tuple of arguments.

The map () function of the Python 3 series does not have this behavior (reference). In fact, if None is given to the first argument function, an error will occur in Python3 series.

Python 3.5.2 (default, Jul 29 2016, 11:13:25)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> list(map(None, [1, 2, 3]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

Recommended Posts

__future__ module and future_builtins module
Python debug and test module
Cooperation between python module and API
[Code] Module and Python version output
[Python] About Executor and Future classes
Python3 socket module and socket communication flow
Implementation module "deque" in queue and Python
Coexistence of Flask logging and other module logging
Module import and exception handling in python