Inter-module globals

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

    #1

    Inter-module globals

    Hi,

    I want to use globals that are immediately visible in all modules. My
    attempts to use "global" haven't worked. Suggestions?

    Anton
  • Jorge Godoy

    #2
    Re: Inter-module globals

    Anton81 <forum@anton.e4 ward.com> writes:
    [color=blue]
    > I want to use globals that are immediately visible in all modules. My
    > attempts to use "global" haven't worked. Suggestions?[/color]

    Use a module and a class variables for that. Import your module and
    read/update class variables as you need them.


    --
    Jorge Godoy <godoy@ieee.org >

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.

    Comment

    • Schüle Daniel

      #3
      Re: Inter-module globals

      Anton81 wrote:[color=blue]
      > Hi,
      >
      > I want to use globals that are immediately visible in all modules. My
      > attempts to use "global" haven't worked. Suggestions?
      >
      > Anton[/color]

      I think a dictionary would work here
      as well as list but not strings and int's

      # module1
      settings = {
      "release" : "1.0",
      "blabla" : None,
      }

      # module2
      import module1 as settings
      print settings.settin gs["release"]
      settings.settin gs["blabla"] = True


      # module3
      import module1 as settings
      settings.settin gs["blabla"] = False

      Regards

      Comment

      • Joel Hedlund

        #4
        Re: Inter-module globals

        > Use a module and a class variables for that.

        I think we could manage a little example too ;-) This one is a fictitious little game project where the user can define a custom graphics directory on the command line.

        Three files: game.py, graphics.py and common.py.

        The common.py file contains that class with class variables that jorge was talking about. Other modules import this as needed.

        The contents of the files follow below.

        So:
        $ python game.py
        /usr/local/mygame/data/gfx
        $ python game.py --gfx-dir moo/cow
        /usr/local/mygame/moo/cow
        $ python game.py --gfx-dir /moo/cow
        /moo/cow

        Hope it helps!
        /Joel Hedlund



        main.py:
        --------------------------------------------------------
        #!/usr/bin/python

        import sys

        import graphics
        from common import Settings

        try:
        i = sys.argv.index( '--gfx-dir')
        except ValueError:
        pass
        else:
        Settings.graphi cs_dir = sys.argv[i + 1]

        print graphics.graphi cs_dir()
        --------------------------------------------------------



        common.py:
        --------------------------------------------------------
        class Settings(object ):
        game_dir = '/usr/local/mygame'
        graphics_dir = 'data/gfx'
        --------------------------------------------------------



        graphics.py:
        --------------------------------------------------------
        import os

        from common import Settings

        def graphics_dir():
        return os.path.join(Se ttings.game_dir , Settings.graphi cs_dir)
        --------------------------------------------------------







        Jorge Godoy wrote:[color=blue]
        > Anton81 <forum@anton.e4 ward.com> writes:
        >
        >[color=green]
        >>I want to use globals that are immediately visible in all modules. My
        >>attempts to use "global" haven't worked. Suggestions?[/color]
        >
        >
        > Use a module and a class variables for that. Import your module and
        > read/update class variables as you need them.
        >
        >[/color]

        Comment

        Working...