__future__ frequently appears in Python modules. I do not understand what __future__ is for and how/when to use it even after reading the Python’s __future__ doc.
Can anyone explain with examples?
A few answers regarding the basic usage of __future__ I’ve received seemed correct.
However, I need to understand one more thing regarding how __future__ works:
The most confusing concept for me is how a current python release includes features for future releases, and how a program using a feature from a future release can be be compiled successfully in the current version of Python.
I am guessing that the current release is packaged with potential features for the future. However, the features are available only by using __future__ because they are not the current standard. Let me know if I am right.
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_statementin 2.5, as thewithkeyword was new and shouldn’t be used as variable names any longer. In order to usewithas a Python keyword in Python 2.5 or older, you will need to use the import from above.Another example is
Without the
__future__stuff, bothprintstatements would print1.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:printbecomes a function in 3.x, losing its special property as a keyword. So it is the other way round.