truly global variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ketulp_baroda@yahoo.com

    truly global variables

    Hi
    Global variables are not truly global in Python ; they are only global
    within a module namespace. I want a variable such that if i change its
    value in any module then it should be reflected in other modules too.
    What I mean is I wanta varibale say i whose initial value is 1.
    If I change value of i in foo.py to say 5 and now I print value of i
    in bar.py ,it should print 5.How to do this??


    --
  • Ben Finney

    #2
    Re: truly global variables

    On 19 Feb 2004 00:47:13 -0800, ketulp_baroda@y ahoo.com wrote:[color=blue]
    > I want a variable such that if i change its value in any module then
    > it should be reflected in other modules too.[/color]

    Fortunately, this nightmare is not possible in Python.

    What you should be doing is implementing the variable in a module that
    is imported wherever that variable is needed. This allows you more
    control over how the variable is handled; you know there is only one
    place where the variable is declared, and any usage of the variable is
    explicitly preceded by the module name.

    One common place where people think they want global variables but
    really want a common module, is configuration variables. Simply make a
    single module whose specific purpose is to hold the configuration
    variables for your application, then use the config variables via that
    module:

    import AppConfig
    open_screen_win dow( AppConfig.windo w_size )
    AppConfig.prefe rred_hostname = None

    The next step would be to make an AppConfig class that handles things
    like persistence of config changes, but that may be more complexity than
    you need.

    --
    \ "As the most participatory form of mass speech yet developed, |
    `\ the Internet deserves the highest protection from governmental |
    _o__) intrusion." -- U.S. District Court Judge Dalzell |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • Peter Hansen

      #3
      Re: truly global variables

      ketulp_baroda@y ahoo.com wrote:[color=blue]
      >
      > Global variables are not truly global in Python ; they are only global
      > within a module namespace. I want a variable such that if i change its
      > value in any module then it should be reflected in other modules too.
      > What I mean is I wanta varibale say i whose initial value is 1.
      > If I change value of i in foo.py to say 5 and now I print value of i
      > in bar.py ,it should print 5.How to do this??[/color]

      Provided you're willing to have a dot in the name of the variable,
      this is actually quite possible, as Ben shows even though he markets
      his approach poorly by saying what you want is not possible. :-)

      If you can explain why you wouldn't want to just use a module to
      contain your globals, we'll be able to suggest alternatives (and
      there are some alternatives, they're just not usually as clean or
      simple).

      -Peter

      Comment

      • Ben Finney

        #4
        Re: truly global variables

        On Thu, 19 Feb 2004 10:31:44 -0500, Peter Hansen wrote:[color=blue]
        > Provided you're willing to have a dot in the name of the variable,[/color]

        Variable names don't have dots in them. A dot separates the variable
        name from its namespace.

        --
        \ "God forbid that any book should be banned. The practice is as |
        `\ indefensible as infanticide." -- Dame Rebecca West |
        _o__) |
        Ben Finney <http://bignose.squidly .org/>

        Comment

        • Peter Hansen

          #5
          Re: truly global variables

          Ben Finney wrote:[color=blue]
          >
          > On Thu, 19 Feb 2004 10:31:44 -0500, Peter Hansen wrote:[color=green]
          > > Provided you're willing to have a dot in the name of the variable,[/color]
          >
          > Variable names don't have dots in them. A dot separates the variable
          > name from its namespace.[/color]

          I know that. You know that. From the point of view of a newbie who
          wants "true globals", it's a moot point...

          -Peter

          Comment

          Working...