With __future__
module's inclusion, you can slowly be accustomed to incompatible changes or to such ones introducing new keywords.
E.g., for using context managers, you had to do from __future__ import with_statement
in 2.5, as the with
keyword was new and shouldn't be used as variable names any longer. In order to use with
as a Python keyword in Python 2.5 or older, you will need to use the import from above.
Another example is
from __future__ import division
print 8/7 # prints 1.1428571428571428
print 8//7 # prints 1
Without the __future__
stuff, both print
statements would print 1
.
The internal difference is that without that import, /
is mapped to the __div__()
method, while with it, __truediv__()
is used. (In any case, //
calls __floordiv__()
.)
Apropos print
: print
becomes a function in 3.x, losing its special property as a keyword. So it is the other way round.
>>> print
>>> from __future__ import print_function
>>> print
<built-in function print>
>>>
The code is in future.c:
future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
...
else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError,
"not a chance");
PyErr_SyntaxLocation(filename, s->lineno);
return 0;
}
Best Answer
It's related to PEP 0401: BDFL Retirement
Barry refers to Barry Warsaw, a well-known Python developer. The
from __future__ import barry_as_FLUFL
basically replaces the!=
operator with<>
.