Using variables across modules

Discussion in 'Python' started by Aaron Scott, Jul 23, 2008.

  1. Aaron Scott

    Aaron Scott Guest

    I'm having some trouble understanding how Python handles variables
    across multiple modules. I've dug through the documentation, but I
    still find myself at a loss.

    When you import a module, are you creating an instance of the
    variables within? For instance, if I have one file, "variables.py",
    which contains "myvar = 0", and I import it into both "foo.py" and
    "bar.py" with the line "from variables import *", and then set myvar
    in "foo.py" and "bar.py" to different values, will each file have a
    different value for myvar? If so, how can I ensure that a change to
    myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
    base?
     
    Aaron Scott, Jul 23, 2008
    #1
    1. Advertising

  2. Aaron Scott

    Uwe Schmitt Guest

    Aaron Scott schrieb:
    > I'm having some trouble understanding how Python handles variables
    > across multiple modules. I've dug through the documentation, but I
    > still find myself at a loss.
    >
    > When you import a module, are you creating an instance of the
    > variables within? For instance, if I have one file, "variables.py",
    > which contains "myvar = 0", and I import it into both "foo.py" and
    > "bar.py" with the line "from variables import *", and then set myvar
    > in "foo.py" and "bar.py" to different values, will each file have a
    > different value for myvar? If so, how can I ensure that a change to
    > myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
    > base?


    Just wirte test code ! Python is cool for checking such things, you do
    do
    not have to compile, you need not write boilerplate main() stuff.

    Greetings, Uwe
     
    Uwe Schmitt, Jul 23, 2008
    #2
    1. Advertising

  3. Aaron Scott

    Aaron Scott Guest

    > Just wirte test code !

    variables.py:

    myvar = 5
    print myvar

    foo.py:

    from variables import *
    def PrintVar():
    print myvar

    bar.py:

    from variables import *
    from foo import *
    print myvar
    myvar = 2
    print myvar
    PrintVar()

    "python bar.py" output:

    5
    5
    2
    5

    .... which is what I was expecting, but not what I want. Obviously,
    each import is creating its own instance of the variable. What I need
    is a way to change myvar from within "bar.py" so that PrintVar()
    returns the new value, even though it's in a different module.
     
    Aaron Scott, Jul 23, 2008
    #3
  4. Aaron Scott

    Jerry Hill Guest

    On Wed, Jul 23, 2008 at 3:06 PM, Aaron Scott
    <> wrote:
    > ... which is what I was expecting, but not what I want. Obviously,
    > each import is creating its own instance of the variable. What I need
    > is a way to change myvar from within "bar.py" so that PrintVar()
    > returns the new value, even though it's in a different module.


    That's what happens when you do "from variables import *", it creates
    those names in the local namespace. If you just import the module,
    then you can do what you want. For example:

    ---variables.py---
    myvar = 5
    print myvar

    ---foo.py---
    import variables
    def PrintVar():
    print variables.myvar

    ---bar.py---
    import variables, foo
    print variables.myvar
    variables.myvar = 2
    print variables.myvar
    foo.PrintVar()

    ---output from running bar.py---
    5
    5
    2
    2

    For more on how the import statement works, see the library reference
    (http://docs.python.org/ref/import.html), or maybe this article on the
    various ways you can import things and the differences between them
    (http://effbot.org/zone/import-confusion.htm)

    --
    Jerry
     
    Jerry Hill, Jul 23, 2008
    #4
  5. Aaron Scott wrote:

    > I'm having some trouble understanding how Python handles variables
    > across multiple modules. I've dug through the documentation, but I
    > still find myself at a loss.
    >
    > When you import a module, are you creating an instance of the
    > variables within? For instance, if I have one file, "variables.py",
    > which contains "myvar = 0", and I import it into both "foo.py" and
    > "bar.py" with the line "from variables import *", and then set myvar
    > in "foo.py" and "bar.py" to different values, will each file have a
    > different value for myvar? If so, how can I ensure that a change to
    > myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
    > base?


    first read this to learn how objects and variables work in Python:

    http://effbot.org/zone/python-objects.htm

    and then read this to learn how from-import works, and when you're
    supposed to use it:

    http://effbot.org/zone/import-confusion.htm

    hope this helps!

    </F>
     
    Fredrik Lundh, Jul 23, 2008
    #5
  6. Aaron Scott

    Aaron Scott Guest

    Aaron Scott, Jul 23, 2008
    #6
    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. Martin Drautzburg

    how to pass globals across modules (wxPython)

    Martin Drautzburg, Dec 19, 2004, in forum: Python
    Replies:
    9
    Views:
    432
    Jorge Luiz Godoy Filho
    Dec 22, 2004
  2. MackS
    Replies:
    1
    Views:
    295
    Patrick Maupin
    Sep 10, 2005
  3. Ben Weintraub

    Disabling modules using Modules/Setup

    Ben Weintraub, Sep 9, 2006, in forum: Python
    Replies:
    0
    Views:
    357
    Ben Weintraub
    Sep 9, 2006
  4. Hendrik van Rooyen
    Replies:
    0
    Views:
    227
    Hendrik van Rooyen
    Nov 3, 2006
  5. Replies:
    2
    Views:
    105
    Steven D'Aprano
    Nov 3, 2013
Loading...

Share This Page