Re: funcs vs vars in global namespace

From: Alex Martelli (aleaxit_at_yahoo.com)
Date: 09/14/04

  • Next message: Tim Golden: "RE: Communication between remote scripts"
    Date: Tue, 14 Sep 2004 15:30:49 +0200
    
    

    David Rysdam <drysdam@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


  • Next message: Tim Golden: "RE: Communication between remote scripts"

    Relevant Pages

    • Re: Best practise implementation for equal by value objects
      ... optional globals ... and locals name space variables, ... would work independent of how I have imported the module under test. ... I just need to figure out if this is right and how to specify the ...
      (comp.lang.python)
    • Re: Best practise implementation for equal by value objects
      ... optional globals ... and locals name space variables, ... I just need to figure out if this is right and how to specify the ... assert t is timedate.tttime ...
      (comp.lang.python)
    • Exec woes
      ... it is a nested function ... Why the complaint about the nestedness? ... to specify the globals() or you get a NameError - global BUILD is not defined. ...
      (comp.lang.python)
    • Re: Journal select globals
      ... How can I journal only select select globals or exclude selected ... Since you didn't specify your platform, I'll chime in with the Cache ... Select "E" for Enable Journaling ...
      (comp.lang.mumps)
    • Re: Need a better understanding on how MRO works?
      ... nclass = new.classobj,globals) ... i.e. the dict of this module object; ... were in globals() when you called new.classobj, ...
      (comp.lang.python)