FAQ
Taken from www.python.org, FAQ 2.3 How do I share global variables
across modules?

config.py:

x = 0 # Default value of the 'x' configuration setting


mod.py:

import config
config.x = 1


main.py:

import config # try removing it
import mod
print config.x

The example, such as shown in the website, works perfectly well.
However, I don't fully understand why I have to import config in
main.py, since it has already been imported by mod.py.

As the website explains, there is only one module namespace for each
module, and mod.py has aleady created the config namespace by
importing it. Why should I import it again in main.py if that
namespace already exists?

If I remove -> import config # try removing it in main.py,
the application does not run

What am I missing?

Search Discussions

  • Patrick Maupin at May 16, 2010 at 10:05 pm

    On May 16, 4:42?pm, vsoler wrote:
    Taken fromwww.python.org, FAQ 2.3 How do I share global variables
    across modules?

    config.py:

    x = 0 ? # Default value of the 'x' configuration setting

    mod.py:

    import config
    config.x = 1

    main.py:

    import config ? ? ? # try removing it
    import mod
    print config.x

    The example, such as shown in the website, works perfectly well.
    However, I don't fully understand why I have to import config in
    main.py, since it has already been imported by mod.py.

    As the website explains, there is only one module namespace for each
    module, and mod.py has aleady created the config namespace by
    importing it. Why should I import it again in main.py if that
    namespace already exists?

    If I remove -> ? import config ? ? ? # try removing it ? ? in main.py,
    the application does not run

    What am I missing?
    What you are missing is that the interpreter has to look *inside* a
    namespace in order to actually find the object associated with a
    name. As you found out, there is a namespace per module. So
    main.py's namespace is where the code in main.py will search for
    variables. If 'mod' imports config, then the 'mod' module's namespace
    is updated with 'config' -> the config module. But the act of 'mod'
    importing 'config' will not alter the namespace of 'main' at all. So
    if you want to access variable 'x' inside 'config' from main you can
    either import config directly into main and access it as config.x, or
    you can import config into mod and import mod into main and access it
    as mod.config.x.

    Regards,
    Pat
  • Vsoler at May 16, 2010 at 10:26 pm

    On 17 mayo, 00:05, Patrick Maupin wrote:
    On May 16, 4:42?pm, vsoler wrote:


    Taken fromwww.python.org, FAQ 2.3 How do I share global variables
    across modules?
    config.py:
    x = 0 ? # Default value of the 'x' configuration setting
    mod.py:
    import config
    config.x = 1
    main.py:
    import config ? ? ? # try removing it
    import mod
    print config.x
    The example, such as shown in the website, works perfectly well.
    However, I don't fully understand why I have to import config in
    main.py, since it has already been imported by mod.py.
    As the website explains, there is only one module namespace for each
    module, and mod.py has aleady created the config namespace by
    importing it. Why should I import it again in main.py if that
    namespace already exists?
    If I remove -> ? import config ? ? ? # try removing it ? ? in main.py,
    the application does not run
    What am I missing?
    What you are missing is that the interpreter has to look *inside* a
    namespace in order to actually find the object associated with a
    name. ?As you found out, there is a namespace per module. ?So
    main.py's namespace is where the code in main.py will search for
    variables. ?If 'mod' imports config, then the 'mod' module's namespace
    is updated with 'config' -> the config module. ?But the act of 'mod'
    importing 'config' will not alter the namespace of 'main' at all. ?So
    if you want to access variable 'x' inside 'config' from main you can
    either import config directly into main and access it as config.x, or
    you can import config into mod and import mod into main and access it
    as mod.config.x.

    Regards,
    Pat
    Thank you Pat, it's very clear.

    However, can I be 100% sure that,no matter how I access variable
    'x' (with config.x or mod.config.x) it is always the same 'x'. I mean
    that either reference of 'x' points to the same id(memory position)?

    Thank you
  • James Mills at May 16, 2010 at 10:38 pm

    On Mon, May 17, 2010 at 8:26 AM, vsoler wrote:
    However, can I be 100% sure that,no matter how I access variable
    'x' (with config.x or mod.config.x) it is always the same 'x'. I mean
    that either reference of 'x' points to the same id(memory position)?
    Yes it does unless you re-assign it.

    --James
  • Vsoler at May 16, 2010 at 10:45 pm

    On 17 mayo, 00:38, James Mills wrote:
    On Mon, May 17, 2010 at 8:26 AM, vsoler wrote:
    However, can I be 100% sure that,no matter how I access variable
    'x' (with config.x or mod.config.x) it is always the same 'x'. I mean
    that either reference of 'x' points to the same id(memory position)?
    Yes it does unless you re-assign it.

    --James
    Understood, thank you very much

    Vicente Soler
  • Patrick Maupin at May 16, 2010 at 10:52 pm

    On May 16, 5:38?pm, James Mills wrote:
    On Mon, May 17, 2010 at 8:26 AM, vsoler wrote:
    However, can I be 100% sure that,no matter how I access variable
    'x' (with config.x or mod.config.x) it is always the same 'x'. I mean
    that either reference of 'x' points to the same id(memory position)?
    Yes it does unless you re-assign it.

    --James

    To expand a bit on what James is saying:

    If, for example, inside your main module, you got tired of typing
    "mod.config.x" everywhere you were using it, and decided that you
    could make a local reference to the same variable:

    x = mod.config.x

    Now, whenever you use just plain x inside the main module, you are
    also referencing the exact same object, *until* some other function
    decides to do:

    mod.config.x = y

    At this point in time, the 'x' inside main references the object that
    mod.config.x originally referenced, but mod.config.x now references a
    different object.

    Unlike C, for example, where the assignment operator physically places
    an item into a specific memory location (either fixed globally or
    within a stack frame), the assignment operator in python simply stores
    a key/value pair into a namespace dictionary. So whenever you
    retrieve a value from the dictionary using that key, you will get the
    value that was last associated with that key.

    So, 'mod.config.x' will first retrieve the object associated with the
    key 'mod' from the main module's namespace dictionary, then will
    retrieve the object associated with the key 'config' from that
    module's namespace dictionary, then will retrieve the object
    associated with the key 'x' from that module's namespace dictionary.
    Unless you later modify any of those key/value pairs, subsequent
    retrieval will always result in the same final value.

    Regards,
    Pat
  • Vsoler at May 16, 2010 at 11:21 pm

    On 17 mayo, 00:52, Patrick Maupin wrote:
    On May 16, 5:38?pm, James Mills wrote:
    On Mon, May 17, 2010 at 8:26 AM, vsoler wrote:
    However, can I be 100% sure that,no matter how I access variable
    'x' (with config.x or mod.config.x) it is always the same 'x'. I mean
    that either reference of 'x' points to the same id(memory position)?
    Yes it does unless you re-assign it.
    --James
    To expand a bit on what James is saying:

    If, for example, inside your main module, you got tired of typing
    "mod.config.x" everywhere you were using it, and decided that you
    could make a local reference to the same variable:

    x = mod.config.x

    Now, whenever you use just plain x inside the main module, you are
    also referencing the exact same object, *until* some other function
    decides to do:

    mod.config.x = y

    At this point in time, the 'x' inside main references the object that
    mod.config.x originally referenced, but mod.config.x now references a
    different object.

    Unlike C, for example, where the assignment operator physically places
    an item into a specific memory location (either fixed globally or
    within a stack frame), the assignment operator in python simply stores
    a key/value pair into a namespace dictionary. ?So whenever you
    retrieve a value from the dictionary using that key, you will get the
    value that was last associated with that key.

    So, 'mod.config.x' will first retrieve the object associated with the
    key 'mod' from the main module's namespace dictionary, then will
    retrieve the object associated with the key 'config' from that
    module's namespace dictionary, then will retrieve the object
    associated with the key 'x' from that module's namespace dictionary.
    Unless you later modify any of those key/value pairs, subsequent
    retrieval will always result in the same final value.

    Regards,
    Pat
    Really interesting, it helps a lot.

    Thank you
  • Rhodri James at May 16, 2010 at 10:23 pm

    On Sun, 16 May 2010 22:42:40 +0100, vsoler wrote:

    Taken from www.python.org, FAQ 2.3 How do I share global variables
    across modules?

    config.py:

    x = 0 # Default value of the 'x' configuration setting


    mod.py:

    import config
    config.x = 1


    main.py:

    import config # try removing it
    import mod
    print config.x

    The example, such as shown in the website, works perfectly well.
    However, I don't fully understand why I have to import config in
    main.py, since it has already been imported by mod.py.
    Globals are only global to a module. As you can see above, they
    don't magically appear in the namespace of the module you import
    them into (not unless you do something careless like "from config
    import *", at which point you deserve all the confusion you're
    going to get). In both mod.py and main.py you get at the global
    x through its module: config.x

    Now as you say, main.py imports mod. In exactly the same way as
    mod imports config. *Exactly*. We get at the contents of mod
    (the things in its namespace) in the same way: mod.thing. When
    mod imports config it imports it into its own namespace only;
    there is no magic that makes it appear in the namespace of anything
    importing mod any more than happens for anything else.

    If you want to access config in main.py without importing it
    directly because you know that (in this case) mod has already
    imported it, you have to access it through the module that did
    the importing. Instead of "config.x", in main it would be
    "mod.config.x".

    --
    Rhodri James *-* Wildebeeste Herder to the Masses

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 16, '10 at 9:42p
activeMay 16, '10 at 11:21p
posts8
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase