global variables

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

    #1

    global variables

    Hi,

    is it possible to create 'global' variables that can be seen in all
    other classes?

    Alex

  • Steve Holden

    #2
    Re: global variables

    alex wrote:
    [color=blue]
    > Hi,
    >
    > is it possible to create 'global' variables that can be seen in all
    > other classes?
    >
    > Alex
    >[/color]
    Not sensibly, though you can mess around with the __builtin__ namespace
    to make values accessible without qualification.

    The usual solution is to maintain a config module that establishes
    default settings for configuration variables. Other modules that import
    config can access (and change) those values using

    config.name = value

    and so on. Hope this help.

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/

    Comment

    • M.E.Farmer

      #3
      Re: global variables

      alex wrote:[color=blue]
      > Hi,
      >
      > is it possible to create 'global' variables that can be seen in all
      > other classes?
      >
      > Alex[/color]
      Hello,
      What about using a class?

      Py> class globalVar:
      .... pass

      Py> globals = globalVar()

      Now you can assign 'variables' to it.
      And use it anywhere you need it.

      Py> globals.image_h eight = (255,777)
      Py> globals.image_m ode = 'RGB'
      Py> globals.image_n ames = ['this.jpg', that.jpg']
      etc...
      hth,
      M.E.Farmer

      Comment

      • Steven Bethard

        #4
        Re: global variables

        M.E.Farmer wrote:[color=blue]
        > alex wrote:[color=green]
        >> is it possible to create 'global' variables that can be seen in all
        >> other classes?[/color]
        >
        > What about using a class?
        >
        > Py> class globalVar:
        > ... pass
        >
        > Py> globals = globalVar()[/color]

        Probably naming it something other than 'globals' would be a good idea
        -- otherwise you'll hide the builtin globals() function.

        But I agree that the attributes of a class instance (as you suggest) or
        the attributes of a module (as Steve Holden suggests) is probably the
        right way to go.

        Steve

        Comment

        • Larry Bates

          #5
          Re: global variables

          One way to to this is by using keyword args:

          class a:
          def __init__(self, arg1, arg2, **kwargs):
          #
          # Dictionary kwargs will have keyword, value pairs
          # that can be used as global space.
          #
          self.arg1=arg1
          self.arg2=arg2
          self.__dict__.u pdate(kwargs)
          return

          class b:
          def __init__(self, arg1, arg2, **kwargs):
          #
          # Dictionary kwargs will have keyword, value pairs
          # that can be used as global space.
          #
          self.__dict__.u pdate(kwargs)
          self.a=a(arg1, arg2, **kwargs)
          return

          class c:
          def __init__(self, arg1, arg2, **kwargs):
          #
          # Dictionary kwargs will have keyword, value pairs
          # that can be used as global space.
          #
          self.__dict__.u pdate(kwargs)
          self.b=b(arg1, arg2, **kwargs)
          return

          globals={'globa l1':1, 'global2':2, 'global3':3, 'global4':4}
          C=c(1, 2, **globals)

          you will have global1, global2, global3, and global4 attributs
          in all classes. If you don't want the attributes, just access
          to the values, delete the self.__dict__.u pdate(kwargs) lines.

          Larry Bates

          alex wrote:[color=blue]
          > Hi,
          >
          > is it possible to create 'global' variables that can be seen in all
          > other classes?
          >
          > Alex
          >[/color]

          Comment

          • M.E.Farmer

            #6
            Re: global variables

            Steve,
            Yes I agree ;) Never use builtin names.
            I know better but missed it somehow.
            I apologize for any confusion I may have caused.
            Thank you Steve for the correction.
            M.E.Farmer

            Steven Bethard wrote:[color=blue]
            > M.E.Farmer wrote:[color=green]
            > > alex wrote:[color=darkred]
            > >> is it possible to create 'global' variables that can be seen in[/color][/color][/color]
            all[color=blue][color=green][color=darkred]
            > >> other classes?[/color]
            > >
            > > What about using a class?
            > >
            > > Py> class globalVar:
            > > ... pass
            > >
            > > Py> globals = globalVar()[/color]
            >
            > Probably naming it something other than 'globals' would be a good[/color]
            idea[color=blue]
            > -- otherwise you'll hide the builtin globals() function.
            >
            > But I agree that the attributes of a class instance (as you suggest)[/color]
            or[color=blue]
            > the attributes of a module (as Steve Holden suggests) is probably the
            > right way to go.
            >
            > Steve[/color]

            Comment

            • M.E.Farmer

              #7
              Re: global variables

              Ok it has been a long day,
              In my reply to Steven Bethard , Steve should read Steven ;)

              M.E.Farmer

              Comment

              • Nick Coghlan

                #8
                Re: global variables

                A Steve wrote:[color=blue]
                > A Steve wrote:
                >[color=green]
                >> A Steve wrote:
                >>[/color][/color]

                There we go, much clearer ;)

                Cheers,
                Nick.

                --
                Nick Coghlan | ncoghlan@email. com | Brisbane, Australia
                ---------------------------------------------------------------

                Comment

                • Steve TZOTZIOY Georgiou

                  #9
                  Re: [OT] PEP 8j, pythonista naming style

                  On Fri, 04 Feb 2005 23:56:44 +1000, rumours say that Steve Coghlan
                  <scoghlan@iinet .net.au> might have written:
                  [color=blue]
                  >A Steve wrote:[color=green]
                  >> A Steve wrote:
                  >>[color=darkred]
                  >>> A Steve wrote:
                  >>>[/color][/color]
                  >
                  >There we go, much clearer ;)[/color]

                  Indeed. I recall some Dan Perl who was advised to change his name to a more
                  pythonic one, but now I see he misinterpreted the advice.

                  Am I assimilated or what?
                  --
                  TZOTZIOY, I speak England very best.
                  "Be strict when sending and tolerant when receiving." (from RFC1958)
                  I really should keep that in mind when talking with people, actually...

                  Comment

                  Working...