access interactive namespace from module (shared namespace?)

Discussion in 'Python' started by Ulrich Dorda, May 25, 2008.

  1. Ulrich Dorda

    Ulrich Dorda Guest

    I've got a probably embarrassing trivial problem with namespaces, but couldn't solve it
    myself nor find an answer in the net. Hopefully one of you guys can help me.

    What I want to do:
    Use the interactive shell and e.g define the variable a there.
    Then load a module and access a from within.

    e.g file "utest.py"

    def doit():
    print 2*a

    in the shell:

    import utest
    a=3
    utest.doit() <- I want this to print 2*a, but of course obtain: <type
    exceptions.NameError'>: global name 'a' is not defined

    Any change I do to a in the shell should be seen from the doit() function, any variable
    assignment I do in the doit() function should be seen in the shell. I guess it's somehow a
    namespace sharing.

    Actually the function doit() will contain an eval() function that should evaluate a (via a
    gui) dynamically inserted expression.

    Any one got a clue? (a clue what I try to say and how to help?!)

    Thanks a lot in advance!!

    Ulrich
     
    Ulrich Dorda, May 25, 2008
    #1
    1. Advertising

  2. Ulrich Dorda

    TeroV Guest

    Ulrich Dorda wrote:
    > I've got a probably embarrassing trivial problem with namespaces, but
    > couldn't solve it myself nor find an answer in the net. Hopefully one of
    > you guys can help me.
    >
    > What I want to do:
    > Use the interactive shell and e.g define the variable a there.
    > Then load a module and access a from within.
    >
    > e.g file "utest.py"
    >
    > def doit():
    > print 2*a
    >
    > in the shell:
    >
    > import utest
    > a=3
    > utest.doit() <- I want this to print 2*a, but of course obtain: <type
    > exceptions.NameError'>: global name 'a' is not defined
    >
    > Any change I do to a in the shell should be seen from the doit()
    > function, any variable assignment I do in the doit() function should be
    > seen in the shell. I guess it's somehow a namespace sharing.
    >
    > Actually the function doit() will contain an eval() function that should
    > evaluate a (via a gui) dynamically inserted expression.
    >
    > Any one got a clue? (a clue what I try to say and how to help?!)
    >
    > Thanks a lot in advance!!
    >
    > Ulrich
    >
    >


    Here is one way

    #utest.py:
    def doit(valuemap):
    print 2*valuemap['a']

    Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
    [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a = 4
    >>> import utest
    >>> utest.doit(locals())

    8
    >>>
     
    TeroV, May 25, 2008
    #2
    1. Advertising

  3. Ulrich Dorda

    Guest

    Thanks for the reply,

    Of course the suggested solution is working and good, but a bit
    complicated. The module/function where i need to access the variable
    value from the interactive shell is burried quite deep and I would
    nedd to hand the locals() quite often from one module to another.
    Furthermore it makes the call function slightly more complicated, as
    the locals()-argunment has to be given every time.

    I was hoping for something a bit different: If I wanted to access a
    value b from another module "utest2.py", I would simply need to type
    in utest.py: import utest2; print 2*utest2.b
    Isn't there a name for the interactive namespace (like here the
    utest2), which I can use to access the variable without handing the
    whole dictionary?

    Cheers,

    Ulrich
     
    , May 25, 2008
    #3
  4. Ulrich Dorda

    Peter Otten Guest

    Ulrich Dorda wrote:

    > I've got a probably embarrassing trivial problem with namespaces, but
    > couldn't solve it myself nor find an answer in the net. Hopefully one of
    > you guys can help me.
    >
    > What I want to do:
    > Use the interactive shell and e.g define the variable a there.
    > Then load a module and access a from within.
    >
    > e.g file "utest.py"
    >
    > def doit():
    > print 2*a
    >
    > in the shell:
    >
    > import utest
    > a=3
    > utest.doit() <- I want this to print 2*a, but of course obtain: <type
    > exceptions.NameError'>: global name 'a' is not defined
    >
    > Any change I do to a in the shell should be seen from the doit() function,
    > any variable assignment I do in the doit() function should be seen in the
    > shell. I guess it's somehow a namespace sharing.
    >
    > Actually the function doit() will contain an eval() function that should
    > evaluate a (via a gui) dynamically inserted expression.
    >
    > Any one got a clue? (a clue what I try to say and how to help?!)
    >
    > Thanks a lot in advance!!


    While the sane approach to this is

    def doit(a):
    print 2 * a

    here is an insane one:

    import sys

    def f(): pass
    function = type(f)

    def snatch_globals(f):
    def g(*args, **kw):
    return function(f.func_code, sys._getframe(1).f_globals)(*args,
    **kw)
    return g

    @snatch_globals
    def doit():
    print 2 * a

    Peter
     
    Peter Otten, May 25, 2008
    #4
  5. Ulrich Dorda

    Guest

    Thanks a lot to all!

    Apart from obtaining the solution I was searching for, I learned a lot
    by studying your answers!

    Cheers,

    Ulrich
     
    , May 25, 2008
    #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. DJ Dev
    Replies:
    3
    Views:
    16,163
    Gandalf
    Feb 8, 2004
  2. Joe Fallon
    Replies:
    3
    Views:
    775
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  3. ben
    Replies:
    3
    Views:
    522
    Kevin Spencer
    Nov 15, 2004
  4. WJ
    Replies:
    2
    Views:
    510
  5. tshad
    Replies:
    11
    Views:
    830
    tshad
    May 27, 2005
Loading...

Share This Page