funcs vs vars in global namespace

Alex Martelli aleaxit at yahoo.com
Tue Sep 14 09:30:49 EDT 2004


David Rysdam <drysdam at ll.mit.edu> wrote:
   ...
> > You're misunderstanding Python globals: they are per-module, not "across
> > all modules".
> 
> OK, I can kind of understand that.  I'm not sure what the point of being
> able to specify the globals for an eval() is then, but if it doesn't do
> what I want it doesn't do what I want.

The point is clearer when what you're passing to eval is an expression:

eval('a+b', dict(a=23, b=99)).


> What I want to do is be able to run multiple scripts that use the same
> global variable and function names but be able to have my master script
> define those variables and functions programmatically.  Can I do that
> with the rexec sandbox stuff?  Or is rexec more about keeping the 
> exec()'d code from doing things than specifying precisely what it can do?

rexec has been removed because it did not offer the security it
purported to offer (it was about keeping code from doing bad things, but
it reallly didn't), sigh.

You can do what you want for example by setting the values you want in
the module object of the function you're calling -- that module object's
dictionary is the function's global namespace.  Say:

sub_module = __import__(which_one_this_time)
vars(sub_module).update(which_dict_this_time)
print sub_module.the_function(23)

There are other ways, but few are as direct as this one.


Alex



More information about the Python-list mailing list