Global variables in modules/functions

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

    #1

    Global variables in modules/functions

    I'm trying to write program with a main.py and several functions
    distributed in auxiliary *.py files. I've looked through the archives on
    the 'global' keyword, but I'm still not sure how to get this right. I
    want to be able to have some variables that can be accessed/changed by
    many modules/functions.

    Here's an example.

    ____________
    main.py
    ____________
    import change
    n = 1
    change.change_n ()
    print n

    ____________
    change.py
    ____________
    def change_n():
    global n
    n = 2


    Running main.py gives this: 1. So n is not being changed when the
    function is called. I've tried adding a 'import from main *' line to
    change.py, but this only gives an error message.

    Any ideas?

    Thanks


    --
    Aaron Deskins
    Graduate Student
    Chemical Engineering
    Purdue University
  • Michel Claveau - abstraction méta-galactique non t

    #2
    Re: Global variables in modules/functions

    Hi !

    It is extraordinary: I have just had the same problem, there is seulemet
    one hour!
    For the moment, I prefix the variable; but I will read with interest the
    reponses with your message.

    @-salutations
    --
    Michel Claveau



    Comment

    • Peter Otten

      #3
      Re: Global variables in modules/functions

      Aaron Deskins wrote:
      [color=blue]
      > I'm trying to write program with a main.py and several functions
      > distributed in auxiliary *.py files. I've looked through the archives on
      > the 'global' keyword, but I'm still not sure how to get this right. I
      > want to be able to have some variables that can be accessed/changed by
      > many modules/functions.
      >
      > Here's an example.
      >
      > ____________
      > main.py
      > ____________
      > import change
      > n = 1
      > change.change_n ()
      > print n
      >
      > ____________
      > change.py
      > ____________
      > def change_n():
      > global n
      > n = 2
      >
      >
      > Running main.py gives this: 1. So n is not being changed when the
      > function is called. I've tried adding a 'import from main *' line to[/color]

      The correct syntax is

      from main import *

      This will only copy the binding
      n = 1
      and a following
      n = 2
      assignment will not affect n in the main module.
      [color=blue]
      > change.py, but this only gives an error message.
      >
      > Any ideas?[/color]

      A clean way to share data between different modules is to introduce another
      module and use a qualified name to access data in that module:

      shared.py
      n = 1

      main.py
      import shared
      import change

      change.change_n ()
      print n

      change.py
      import shared
      def change_n():
      shared.n = 2


      However, I'm not convinced that you really need that kind of global data at
      all:

      main.py
      import change
      n = 1
      n = change.change_n ()
      print n

      change.py
      def change_n():
      return 2

      is clearly a superior design.

      Finally, what you originally requested is not impossible, just bad:

      change.py
      import sys

      def change_n():
      sys._getframe(1 ).f_globals["n"] = 2

      Peter




      Comment

      • Peter Otten

        #4
        Re: Global variables in modules/functions

        Peter Otten wrote:
        [color=blue]
        > A clean way to share data between different modules is to introduce
        > another module and use a qualified name to access data in that module:
        >
        > shared.py
        > n = 1
        >
        > main.py
        > import shared
        > import change
        >
        > change.change_n ()
        > print n[/color]

        Oops, that should be

        print shared.n
        [color=blue]
        > change.py
        > import shared
        > def change_n():
        > shared.n = 2
        >[/color]

        Comment

        • Erik Max Francis

          #5
          Re: Global variables in modules/functions

          Aaron Deskins wrote:
          [color=blue]
          > I'm trying to write program with a main.py and several functions
          > distributed in auxiliary *.py files. I've looked through the archives on
          > the 'global' keyword, but I'm still not sure how to get this right. I
          > want to be able to have some variables that can be accessed/changed by
          > many modules/functions.[/color]

          Globals aren't common to all modules. You can think of globals as
          module-specific. That is, the global n is not shared between your two
          modules. It will be in one of the modules, and the other module can
          then reference it explicitly. Something like (untested):

          main.py
          -------
          import change
          change.n = 1
          change.change_n ()
          print change.n

          change.py
          ---------
          n = 0 # here's the actual global
          def change_n():
          global n
          n = 2

          --
          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
          San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
          They have rights who dare defend them.
          -- Roger Baldwin

          Comment

          • Scott David Daniels

            #6
            Re: Global variables in modules/functions

            Aaron Deskins wrote:[color=blue]
            > I'm trying to write program with a main.py and several functions
            > distributed in auxiliary *.py files. I've looked through the archives on
            > the 'global' keyword, but I'm still not sure how to get this right. I
            > want to be able to have some variables that can be accessed/changed by
            > many modules/functions.[/color]
            The easiest way to do this is to create a module named "globals", and
            have any modules wanting to work like:

            import globals
            ....
            def change_n():
            globals.n = 2

            Here's an example.
            ____________
            main.py
            ____________
            import globals, change
            globals.n = 1
            change.change_n ()
            print globals.n
            ____________
            change.py
            ____________
            import globals
            def change_n():
            globals.n = 2

            There are ways to force python into doing what you want, but they are
            fraught with peril.

            --Scott David Daniels
            Scott.Daniels@A cm.Org

            Comment

            • Jeff Shannon

              #7
              Re: Global variables in modules/functions

              Scott David Daniels wrote:
              [color=blue]
              > The easiest way to do this is to create a module named "globals", and
              > have any modules wanting to work like:[/color]


              .... except that you might want to use a different name, to avoid
              shadowing the globals() builtin function. :)

              Jeff Shannon
              Technician/Programmer
              Credit International

              Comment

              • Aaron Deskins

                #8
                Re: Global variables in modules/functions

                Thanks! That works great.

                Scott David Daniels wrote:
                [color=blue]
                >
                > The easiest way to do this is to create a module named "globals", and
                > have any modules wanting to work like:
                >
                > import globals
                > ...
                > def change_n():
                > globals.n = 2
                >[/color]

                Comment

                Working...