Using variables across modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron Scott

    Using variables across modules

    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?
  • Uwe Schmitt

    #2
    Re: Using variables across modules



    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

    Comment

    • Aaron Scott

      #3
      Re: Using variables across modules

      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.

      Comment

      • Jerry Hill

        #4
        Re: Using variables across modules

        On Wed, Jul 23, 2008 at 3:06 PM, Aaron Scott
        <aaron.hildebra ndt@gmail.comwr ote:
        ... 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

        Comment

        • Fredrik Lundh

          #5
          Re: Using variables across modules

          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:



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



          hope this helps!

          </F>

          Comment

          • Aaron Scott

            #6
            Re: Using variables across modules

            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!
            >
            Awesome. After reading those two pages, I was able to correct the code
            and get things up and running. Thanks!

            Aaron

            Comment

            Working...