getting global variables from dictionary

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

    getting global variables from dictionary

    global_vars.py has the global variables
    set_var.py changes one of the values on the global variables (don't
    close it or terminate)
    get_var.py retrieves the recently value changed (triggered right after
    set_var.py above)

    Problem: get_var.py retrieves the old value, the built-in one but not
    the recently changed value in set_var.py.

    What am I doing wrong?

    ----global_vars.py---
    #!/usr/bin/python

    class Variables :
    def __init__(self) :
    self.var_dict = {"username": "original username"}


    ---set_var.py ---
    #!/usr/bin/python

    import time
    import global_vars

    global_vars.Var iables().var_di ct["username"] = "new username"
    time.sleep(10) #give enough time to trigger get_var.py


    ---get_var.py ---
    #!/usr/bin/python
    import global_vars
    print global_vars.Var iables().var_di ct.get("usernam e")
  • Aaron \Castironpi\ Brady

    #2
    Re: getting global variables from dictionary

    On Sep 26, 9:01 pm, icarus <rsa...@gmail.c omwrote:
    global_vars.py has the global variables
    set_var.py changes one of the values on the global variables (don't
    close it or terminate)
    get_var.py retrieves the recently value changed (triggered right after
    set_var.py above)
    >
    Problem: get_var.py retrieves the old value, the built-in one but not
    the recently changed value in set_var.py.
    >
    What am I doing wrong?
    >
    ----global_vars.py---
    #!/usr/bin/python
    >
    class Variables :
            def __init__(self) :
                    self.var_dict = {"username": "original username"}
    >
    ---set_var.py ---
    #!/usr/bin/python
    >
    import time
    import global_vars
    >
    global_vars.Var iables().var_di ct["username"] = "new username"
    time.sleep(10)   #give enough time to trigger get_var.py
    >
    ---get_var.py ---
    #!/usr/bin/python
    import global_vars
    print global_vars.Var iables().var_di ct.get("usernam e")
    Are these separate processes?

    Comment

    • Chris Rebert

      #3
      Re: getting global variables from dictionary

      When you do "Variables( )" in your code, you're making a new instance
      of that class that has no relation to any other instances. This is the
      cause of your problem. To just reference a class, use just
      "Variables" . But that doesn't help in this case because var_dict is an
      instance variable, not a class variable.

      On Fri, Sep 26, 2008 at 7:01 PM, icarus <rsarpi@gmail.c omwrote:
      global_vars.py has the global variables
      No it doesn't, it just defines a class. The class itself (but NOT its
      instances) is a module-level global.
      set_var.py changes one of the values on the global variables (don't
      close it or terminate)
      No, it just instanciates the Variables class and then manipulates the
      instance, which is then GC-ed because it's no longer referenced
      anywhere, even in set_var.
      get_var.py retrieves the recently value changed (triggered right after
      set_var.py above)
      No, it creates an entirely new instance of Variables and then fetches
      the value from that instance (which is still using the default value
      because this new instance has never been modified).
      >
      Problem: get_var.py retrieves the old value, the built-in one but not
      the recently changed value in set_var.py.
      >
      What am I doing wrong?
      Try just making var_dict a module-level variable in global_vars.py and
      then manipulating that rather than this unnecessary mucking about with
      Variables(). Alternatively, make var_dict a *class* variable of
      Variables by removing it from __init__ and just putting 'var_dict =
      {"username": "original username"}' in the raw class body of Variables;
      And then remove the parentheses after Variables as I mentioned in the
      beginning.

      Regards,
      Chris
      >
      ----global_vars.py---
      #!/usr/bin/python
      >
      class Variables :
      def __init__(self) :
      self.var_dict = {"username": "original username"}
      >
      >
      ---set_var.py ---
      #!/usr/bin/python
      >
      import time
      import global_vars
      >
      global_vars.Var iables().var_di ct["username"] = "new username"
      time.sleep(10) #give enough time to trigger get_var.py
      >
      >
      ---get_var.py ---
      #!/usr/bin/python
      import global_vars
      print global_vars.Var iables().var_di ct.get("usernam e")
      --

      >
      --
      Follow the path of the Iguana...

      Comment

      • Ben Finney

        #4
        Re: getting global variables from dictionary

        icarus <rsarpi@gmail.c omwrites:
        global_vars.py has the global variables
        set_var.py changes one of the values on the global variables (don't
        close it or terminate)
        get_var.py retrieves the recently value changed (triggered right after
        set_var.py above)
        >
        Problem: get_var.py retrieves the old value, the built-in one but
        not the recently changed value in set_var.py.
        That's because you're making a new instance each time; each instance
        carries its own state.

        For a collection of attributes that should share state, probably the
        simplest way is to use attributes of a module.
        ----global_vars.py---
        #!/usr/bin/python
        >
        class Variables :
        def __init__(self) :
        self.var_dict = {"username": "original username"}
        These aren't "global variables"; they still need to be imported, like
        anything else from a module. Better to name the module by intent; e.g.
        if these are configuration settings, a module name of 'config' might
        be better.

        Also, this module presumably isn't intended to be run as a program;
        don't put a shebang line (the '#!' line) on files that aren't run as
        programs.

        ===== config.py =====
        # -*- coding: utf-8 -*-

        # Name of the front-end user
        username = "original username"

        # Amount of wortzle to deliver
        wortzle_amount = 170
        =====
        ---set_var.py ---
        #!/usr/bin/python
        >
        import time
        import global_vars
        >
        global_vars.Var iables().var_di ct["username"] = "new username"
        time.sleep(10) #give enough time to trigger get_var.py
        ===== set_config.py =====
        # -*- coding: utf-8 -*-

        import config

        def set_user():
        config.username = "new username"
        =====
        ---get_var.py ---
        #!/usr/bin/python
        import global_vars
        print global_vars.Var iables().var_di ct.get("usernam e")
        ===== get_config.py =====
        # -*- coding: utf-8 -*-

        import config

        def get_user():
        return config.username
        =====


        The 'config' module, imported by both of the other modules, maintains
        state:
        >>import config
        >>print config.username
        original username
        >>import set_config
        >>set_config.se t_user()
        >>print config.username
        new username
        >>import get_config
        >>print get_config.get_ user()
        new username

        --
        \ “You can't have everything; where would you put it?” —Steven |
        `\ Wright |
        _o__) |
        Ben Finney

        Comment

        • George Sakkis

          #5
          Re: getting global variables from dictionary

          On Sep 26, 10:01 pm, icarus <rsa...@gmail.c omwrote:
          global_vars.py has the global variables
          set_var.py changes one of the values on the global variables (don't
          close it or terminate)
          get_var.py retrieves the recently value changed (triggered right after
          set_var.py above)
          >
          Problem: get_var.py retrieves the old value, the built-in one but not
          the recently changed value in set_var.py.
          >
          What am I doing wrong?
          >
          ----global_vars.py---
          #!/usr/bin/python
          >
          class Variables :
                  def __init__(self) :
                          self.var_dict = {"username": "original username"}
          >
          ---set_var.py ---
          #!/usr/bin/python
          >
          import time
          import global_vars
          >
          global_vars.Var iables().var_di ct["username"] = "new username"
          time.sleep(10)   #give enough time to trigger get_var.py
          >
          ---get_var.py ---
          #!/usr/bin/python
          import global_vars
          print global_vars.Var iables().var_di ct.get("usernam e")
          First off, you don't import the set_var module anywhere; how do you
          expect the value to change? Second, every time you do
          "global_vars.Va riables()" you create a brand new Variables() instance,
          initialized with the original var_dict. The Variables() instance you
          create at set_var.py is discarded in the very next line. Third, I have
          no idea why you put the "time.sleep(10) " there.

          By the way, Python is not Java; you don't have to make classes for
          everything. A working version of your example would be:

          ----global_vars.py---

          var_dict = {"username": "original username"}

          ---set_var.py ---
          import global_vars
          global_vars.var _dict["username"] = "new username"

          ---get_var.py ---
          import global_vars
          import set_var
          print global_vars.var _dict.get("user name")

          $ python get_var.py
          new username


          HTH,
          George

          Comment

          Working...