"from module import *" and modifying module's top-level vars

Discussion in 'Python' started by lemke_juergen@yahoo.de, Oct 29, 2006.

  1. Guest

    Hi everyone,

    I define some vars and functions in a "support" module which gets
    called from my
    main app module. Using Python 2.5.

    I import all symbols in the support module at the top of the main
    module through:

    from support import *

    Is there a way for me to modify a top-level ("global"?) variable
    defined in module
    support from the code in the main module and still have those changes
    visible to
    code in the support module?

    Consider the following example:

    # support.py

    globvar = "old"

    def fun():
    print "fun(): globvar==" + globvar

    # main.py

    from support import *
    global globvar # had hoped this would do the trick (as it does when
    placed inside a function call)
    glob = "new"

    print "top level(): glob==" +glob

    fun() # defined inside support

    Calling main.py outputs

    top level(): glob==new
    fun(): glob==old

    How can I get the assignment to globvar performed inside module main.py
    to be
    felt by the code in support.py?

    Thanks in advance!

    Best

    Juergen Lemke
     
    , Oct 29, 2006
    #1
    1. Advertising

  2. schrieb:
    > Hi everyone,
    >
    > I define some vars and functions in a "support" module which gets
    > called from my
    > main app module. Using Python 2.5.
    >
    > I import all symbols in the support module at the top of the main
    > module through:
    >
    > from support import *
    >
    > Is there a way for me to modify a top-level ("global"?) variable
    > defined in module
    > support from the code in the main module and still have those changes
    > visible to
    > code in the support module?


    No. The from foo import * will create local bindings of the
    module-globals in the importing module, and there is no implicit link to
    the module's names.

    This is the main reason why the "from foo import *"-form is frowned
    upon, and you should refrain from using it.

    Use e.g.

    import support as s

    instead, to get a shorter name for referencing the support module.

    Diez
     
    Diez B. Roggisch, Oct 29, 2006
    #2
    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. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    427
  2. Mudcat
    Replies:
    3
    Views:
    971
    Steven D'Aprano
    Jan 14, 2006
  3. pabbu
    Replies:
    8
    Views:
    772
    Marc Boyer
    Nov 7, 2005
  4. Linuxguy123
    Replies:
    7
    Views:
    734
    Paddy O'Loughlin
    Feb 20, 2009
  5. caccolangrifata
    Replies:
    18
    Views:
    430
    Chris Torek
    Jul 22, 2011
Loading...

Share This Page