On 1/8/2010 12:02 PM, Mitchell L Model wrote:
On further reflection, I will add that
what appears to be happening is that during import both the global and
local dictionaries are set to a copy of the globals() from the importing
scope and that copy becomes the value of the module's __dict__ onc
On Jan 8, 2010, at 9:55 AM, "Gabriel Genellina" p...@yahoo.com.ar> wrote:
Ok - short answer or long answer?
Short answer: Emulate how modules work. Make globals() same as
locals(). (BTW, are you sure you want the file to run with the
*same* globals as the caller? It sees the dofile() fun
On Jan 7, 2010, at 10:45 PM, Steven D'Aprano > wrote an extensive answer to my questions about one function
calling another in the same file being exec'd. His suggestion about
printing out locals() and globals() in the various possible places
provided the clues to explain what was going on.
En Thu, 07 Jan 2010 19:47:13 -0300, Mitchell L Model
escribió:
def dofile(filename):
ldict = {'result': None}
with open(filename) as file:
exec(file.read(), globals(), ldict)
print('Result for {}: {}'.format(filename, ldict['result']))
Next I call dof
On Thu, 07 Jan 2010 17:47:13 -0500, Mitchell L Model wrote:
> Next I call dofile() on a slightly more complex file, in which one
> function calls another function defined earlier in the same file.
>
>
> def fn1(val):
> return sum(range(val))
>
> def fn2(arg
Rather than exec the files, why not import them?
I can get both your examples to work using the 'imp' module.
http://docs.python.org/3.1/library/imp.html#module-imp
I used python 2.6.4. Note that 3.1 also has 'importlib' module.
import imp
# the name of the python file written by a user
name
I forgot to offer one answer for question [3] in what I just posted: I
can define all the secondary functions inside one main one and just
call the main one. That provides a separate local scope within the
main function, with the secondary functions defined inside it when
(each time) the ma
[Python 3.1]
I thought I thoroughly understood eval, exec, globals, and locals, but I
encountered something bewildering today. I have some short files I
want to
exec. (Users of my application write them, and the application gives
them a
command that opens a file dialog box and execs the chose