I've been fascinated by the __future__
module - in particular, its ability to change the way statements are parsed in python.
What's most interesting is how doing something like
from __future__ import print_function
Enables you to use print
(and not print_function
, like you would expect any other normal import to do).
I have read What is __future__ in Python used for and how/when to use it, and how it works thoroughly and in particular came across a particular line:
A future statement is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python.
I would love to know the intricacies of what exactly makes this possible. In particular, how something like
from __future__ import division
Can enable true division on python2, while
from __future__ import barry_as_FLUFL
Can enable the <>
syntax on python3 (what I find most funny is that you have to import a feature from "__future__
" for backward compatibility).
Anyway, to summarise, I would like to know how the directive is understood and executed by the compiler when __future__
or its artefacts are imported.