from future import scope problem

M

mykhal

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?
 
T

Terry Reedy

mykhal said:
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.
"
 
M

mykhal

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.
 
M

mykhal

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.
 
J

John Machin

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top