from future import scope problem

Discussion in 'Python' started by mykhal, Mar 12, 2009.

  1. mykhal

    mykhal Guest

    hi,
    importing from __future__ seems to have no effect when invoked in
    local scope using exec statement.
    I supposed

    g = {}
    exec 'from __future__ import division' in g
    eval('1/2', g)

    should yield 0.5, but it yields 0.

    is it OK, or a bug?
     
    mykhal, Mar 12, 2009
    #1
    1. Advertising

  2. mykhal

    Terry Reedy Guest

    mykhal wrote:
    > hi,
    > importing from __future__ seems to have no effect when invoked in
    > local scope using exec statement.
    > I supposed
    >
    > g = {}
    > exec 'from __future__ import division' in g
    > eval('1/2', g)
    >
    > should yield 0.5, but it yields 0.
    >
    > is it OK, or a bug?


    Please to read the fine manual.

    tjr

    ps.

    " future statement must appear near the top of the module. The only
    lines that can appear before a future statement are:

    the module docstring (if any),
    comments,
    blank lines, and
    other future statements.
    "
     
    Terry Reedy, Mar 12, 2009
    #2
    1. Advertising

  3. mykhal

    mykhal Guest

    On Mar 13, 12:46 am, Terry Reedy <> wrote:
    > mykhal wrote:
    > > hi,
    > > importing from __future__ seems to have no effect when invoked in
    > > local scope using exec statement.
    > > I supposed

    >
    > > g = {}
    > > exec 'from __future__ import division' in g
    > > eval('1/2', g)

    >
    > > should yield 0.5, but it yields 0.

    >
    > > is it OK, or a bug?

    >
    > Please to read the fine manual.
    >
    > tjr
    >
    > ps.
    >
    > " future statement must appear near the top of the module. The only
    > lines that can appear before a future statement are:
    >
    > the module docstring (if any),
    > comments,
    > blank lines, and
    > other future statements.
    > "


    as you can see, my future import statement is the very first code of
    the virtual module, using g dictionary as its globals. i know, it can
    be barely called module..
    if it produced SyntaxError in this exec/in usage as well, I'd have no
    questions.
     
    mykhal, Mar 13, 2009
    #3
  4. mykhal

    mykhal Guest

    On Mar 13, 2:58 am, mykhal <> wrote:
    > On Mar 13, 12:46 am, Terry Reedy <> wrote:
    >
    >
    >
    >
    >
    > > mykhal wrote:
    > > > hi,
    > > > importing from __future__ seems to have no effect when invoked in
    > > > local scope using exec statement.
    > > > I supposed

    >
    > > > g = {}
    > > > exec 'from __future__ import division' in g
    > > > eval('1/2', g)

    >
    > > > should yield 0.5, but it yields 0.

    >
    > > > is it OK, or a bug?

    >
    > > Please to read the fine manual.

    >
    > > tjr

    >
    > > ps.

    >
    > > " future statement must appear near the top of the module. The only
    > > lines that can appear before a future statement are:

    >
    > > the module docstring (if any),
    > > comments,
    > > blank lines, and
    > > other future statements.
    > > "

    >
    > as you can see, my future import statement is the very first code of
    > the virtual module, using g dictionary as its globals. i know, it can
    > be barely called module..
    > if it produced SyntaxError in this exec/in usage as well, I'd have no
    > questions.


    now I can see, that following yields the desired division result.

    g = {}
    exec "from __future__ import division; x=1/2" in g

    someone without huge knowledge of Python implementation details might
    expect my original code to produce the same.
     
    mykhal, Mar 13, 2009
    #4
  5. mykhal

    John Machin Guest

    On Mar 13, 1:20 pm, mykhal <> wrote:
    > On Mar 13, 2:58 am, mykhal <> wrote:
    >
    >
    >
    > > On Mar 13, 12:46 am, Terry Reedy <> wrote:

    >
    > > > mykhal wrote:
    > > > > hi,
    > > > > importing from __future__ seems to have no effect when invoked in
    > > > > local scope using exec statement.
    > > > > I supposed

    >
    > > > > g = {}
    > > > > exec 'from __future__ import division' in g
    > > > > eval('1/2', g)

    >
    > > > > should yield 0.5, but it yields 0.

    >
    > > > > is it OK, or a bug?

    >
    > > > Please to read the fine manual.

    >
    > > > tjr

    >
    > > > ps.

    >
    > > > " future statement must appear near the top of the module. The only
    > > > lines that can appear before a future statement are:

    >
    > > > the module docstring (if any),
    > > > comments,
    > > > blank lines, and
    > > > other future statements.
    > > > "

    >
    > > as you can see, my future import statement is the very first code of
    > > the virtual module, using g dictionary as its globals. i know, it can
    > > be barely called module..
    > > if it produced SyntaxError in this exec/in usage as well, I'd have no
    > > questions.

    >
    > now I can see, that following yields the desired division result.
    >
    > g = {}
    > exec "from __future__ import division; x=1/2" in g
    >
    > someone without huge knowledge of Python implementation details might
    > expect my original code to produce the same.


    As might somebody who hadn't read the manual carefully enough before
    mucking about with a combination of "exec" and "from __future__
    import ..." :)

    It's very little to do with "implementation details". "from __future__
    import ..." is documented to be a compile-time directive affecting
    only the module (real or virtual) in which the directive appears.
     
    John Machin, Mar 13, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    963
    Paul Opal
    Oct 11, 2004
  2. Andrew James
    Replies:
    1
    Views:
    981
    Diez B. Roggisch
    Dec 4, 2004
  3. lee

    import from future

    lee, Jan 28, 2007, in forum: Python
    Replies:
    4
    Views:
    339
    Peter Otten
    Jan 29, 2007
  4. _wolf
    Replies:
    5
    Views:
    286
    Gabriel Genellina
    Dec 13, 2008
  5. Ulrich Eckhardt

    from future import pass_function

    Ulrich Eckhardt, Jul 25, 2012, in forum: Python
    Replies:
    48
    Views:
    632
    Ulrich Eckhardt
    Jul 27, 2012
Loading...

Share This Page