Detecting __future__ features

Carsten Haese carsten at
Mon Jul 30 15:08:27 CEST 2007

On Mon, 2007-07-30 at 12:53 +0000, André wrote:
> On Jul 30, 9:39 am, Neil Cerutti <horp... at> wrote:
> > On 2007-07-30, Steven D'Aprano
> >
> > <st... at> wrote:
> > > How would one tell at runtime if a particular feature has been
> > > enabled by the "from __future__ import thing" statement?
> >
> > I don't understand the qualification, "at runtime," you're
> > making. What's wrong with just importing what you want and using
> > it? If it's already been enabled, no harm will come from the
> > import statement.
> >
> I'm not the OP, so perhaps I am missing his intent. However, I can see
> a good reason for asking this question.
> I seem to recall that the "from __future__ import" statement can only
> be done at the beginning of a script.

Incorrect. It must be done at the beginning of the *file*.

>   What if you are designing a
> module meant to be imported, and used by other programs over which you
> have no control?   You can't use "from __future__ import" in your
> module.

Incorrect. You can use a __future__ import in a module as long as you do
it at the beginning of the modul file.

>   So, you may have to find a way to figure out what's been
> done.  (the example given with the division operator is a good one).

No. __future__ directives are scoped to the module. Observe:

$ cat
def f1():
  print 1/2

import f2

$ cat
from __future__ import division

def f2():
  print 1/2

$ python 

As you can see, f1 uses past semantics, f2 uses future semantics. Just
use whatever __future__ directives you need for your module at the
beginning of your module, and everything will just work.


Carsten Haese

More information about the Python-list mailing list