Question about scope

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

    Question about scope

    I have a Globals class.

    In it, I have a variable defined something like this:

    remote_device_e nabled = bool

    In one module, I assign True/False to Globals.remote_ device_enabled.
    Once set, this value never changes.

    In another module, at the top after the imports statements, I tried this:

    from Globals import *

    RDE = Globals.remote_ device_enabled

    This way, I thought that I could just use 'if RDE:'

    Within the functions, however, I get a different value. What am I
    misunderstandin g?

    I tried this at the top of the module (but it didn't word):

    global RDE
    RDE = Globals.remote_ device_enabled

    Of course, within a function, the variable using the same two lines of
    code assigns the correct value to RDE.

    Thank you,

    Total Newbie
  • Lave

    #2
    Re: Question about scope

    Newbie too. I think you shoud qualify Global with the module name.

    On 10/23/08, Pat <Pat@junk.netwr ote:
    I have a Globals class.
    >
    In it, I have a variable defined something like this:
    >
    remote_device_e nabled = bool
    >
    In one module, I assign True/False to Globals.remote_ device_enabled.
    Once set, this value never changes.
    >
    In another module, at the top after the imports statements, I tried this:
    >
    from Globals import *
    >
    from <moduleimport Globals ?
    RDE = Globals.remote_ device_enabled
    >
    Here,

    RDE = <module>.Global s.remote_device _enabled
    This way, I thought that I could just use 'if RDE:'
    >
    Within the functions, however, I get a different value. What am I
    misunderstandin g?
    >
    I tried this at the top of the module (but it didn't word):
    >
    global RDE
    RDE = Globals.remote_ device_enabled
    >
    Of course, within a function, the variable using the same two lines of
    code assigns the correct value to RDE.
    >
    Thank you,
    >
    Total Newbie
    --

    >

    --
    Regards,
    Lave

    Comment

    • Bruno Desthuilliers

      #3
      Re: Question about scope

      Pat a écrit :
      I have a Globals class.
      Not sure it's such a great idea, but anyway... What's the use case for
      this class ? There are perhaps better (or at least more idiomatic)
      solutions...
      In it, I have a variable defined something like this:
      >
      remote_device_e nabled = bool
      Could you show actual code ? It would really help. But it seems your
      'Globals' class is mostly 1/ a singleton and 2/ used for application
      wide settings. Is that right ?
      In one module, I assign True/False to Globals.remote_ device_enabled.
      Directly to the class ?

      Please, once again, provide real code. Well... not necessarily your
      whole code, but at least minimal working code that reproduces the problem.
      Once set, this value never changes.
      >
      In another module, at the top after the imports statements, I tried this:
      >
      from Globals import *
      <ot>
      The convention is to use lower case names for modules (and MixedCase
      names for classes). This avoids confusion between synonym classes and
      modules...
      </ot>
      RDE = Globals.remote_ device_enabled
      >
      This way, I thought that I could just use 'if RDE:'
      >
      Within the functions, however, I get a different value. What am I
      misunderstandin g?
      Not enough informations, and my crystal ball is out for repair. Sorry.
      Perhaps some actual code may help ?-)
      I tried this at the top of the module (but it didn't word):
      >
      global RDE
      Outside a function body, the 'global' statement is a no-op. In Python,
      'global' really means 'module-level', so anything defined at the module
      level is already as global as it can be.
      RDE = Globals.remote_ device_enabled
      >
      Of course, within a function, the variable using the same two lines of
      code assigns the correct value to RDE.
      Sorry Pat, but there's just not enough context for us to guess what's
      wrong. It's easy enough to get it wrong with real code, so trying to
      guess is just a waste of time.

      Comment

      • Pat

        #4
        Re: Question about scope

        Bruno Desthuilliers wrote:
        Pat a écrit :
        >I have a Globals class.
        >
        Not sure it's such a great idea, but anyway... What's the use case for
        this class ? There are perhaps better (or at least more idiomatic)
        solutions...
        >
        >In it, I have a variable defined something like this:
        >>
        >remote_device_ enabled = bool
        >
        Could you show actual code ? It would really help. But it seems your
        'Globals' class is mostly 1/ a singleton and 2/ used for application
        wide settings. Is that right ?
        >
        >In one module, I assign True/False to Globals.remote_ device_enabled.
        >
        Directly to the class ?
        >
        Please, once again, provide real code. Well... not necessarily your
        whole code, but at least minimal working code that reproduces the problem.
        >
        >Once set, this value never changes.
        >>
        >In another module, at the top after the imports statements, I tried this:
        >>
        >from Globals import *
        >
        <ot>
        The convention is to use lower case names for modules (and MixedCase
        names for classes). This avoids confusion between synonym classes and
        modules...
        </ot>
        >
        >RDE = Globals.remote_ device_enabled
        >>
        >This way, I thought that I could just use 'if RDE:'
        >>
        >Within the functions, however, I get a different value. What am I
        >misunderstandi ng?
        >
        Not enough informations, and my crystal ball is out for repair. Sorry.
        Perhaps some actual code may help ?-)
        >
        >I tried this at the top of the module (but it didn't word):
        >>
        >global RDE
        >
        Outside a function body, the 'global' statement is a no-op. In Python,
        'global' really means 'module-level', so anything defined at the module
        level is already as global as it can be.
        >
        >RDE = Globals.remote_ device_enabled
        >>
        >Of course, within a function, the variable using the same two lines of
        >code assigns the correct value to RDE.
        >
        Sorry Pat, but there's just not enough context for us to guess what's
        wrong. It's easy enough to get it wrong with real code, so trying to
        guess is just a waste of time.

        Stripping out the extra variables and definitions, this is all that
        there is.
        Whether or not this technique is *correct* programming is irrelevant. I
        simply want to know why scoping doesn't work like I thought it would.


        ---myGlobals.py file:

        class myGlobals():
        remote_device_e nabled = bool

        ---my initialize.py file:

        from myGlobals import *
        def initialize():
        myGlobals.remot e_device_enable d = True

        ---my main.py file:

        import from myGlobals import *
        RDE = myGlobals.remot e_device_enable d

        def main():
        if RDE: # this will not give me the correct value
        process_device( )





        Comment

        • Steven D'Aprano

          #5
          Re: Question about scope

          On Thu, 23 Oct 2008 11:38:35 -0400, Pat wrote:
          I have a Globals class.
          Well, that's your first mistake. Using global variables in a class is no
          better than using bare global variables. They're still global, and that's
          a problem:



          In it, I have a variable defined something like this:
          >
          remote_device_e nabled = bool
          >
          In one module, I assign True/False to Globals.remote_ device_enabled.
          Once set, this value never changes.
          >
          In another module, at the top after the imports statements, I tried
          this:
          >
          from Globals import *
          That can't work if Globals is a class. I take it you meant that Globals
          is a module. That's still got all the disadvantages of global variables.


          RDE = Globals.remote_ device_enabled
          >
          This way, I thought that I could just use 'if RDE:'
          >
          Within the functions, however, I get a different value. What am I
          misunderstandin g?
          Everything?

          Perhaps it's time to go back to basics and work through the tutorial.

          I tried this at the top of the module (but it didn't word):
          >
          global RDE
          RDE = Globals.remote_ device_enabled
          At the top of a module, the "global" keyword is a no-op, because
          everything at the top of a module is already global.


          --
          Steven

          Comment

          • Steven D'Aprano

            #6
            Re: Question about scope

            On Thu, 23 Oct 2008 21:08:12 -0400, Pat wrote:
            Stripping out the extra variables and definitions, this is all that
            there is.
            Whether or not this technique is *correct* programming is irrelevant.
            Oh rly?

            Well, sure, you can write bad code if you like, and make your actual job
            much harder. No skin off our nose, except when you come along asking for
            free debugging out of the goodness of our hearts.

            I simply want to know why scoping doesn't work like I thought it would.
            >
            >
            ---myGlobals.py file:
            >
            class myGlobals():
            remote_device_e nabled = bool
            Creates a class myGlobals with a class attribute called
            "remote_device_ enabled" which is equal to the class bool.

            Why not just initialise it to True here?

            Why is it a class attribute instead of an instance attribute?

            ---my initialize.py file:
            >
            from myGlobals import *
            Creates a name called "myGlobals" which is local to this module. It is
            bound to the same myGlobals class as defined by myGlobals.py module.

            def initialize():
            myGlobals.remot e_device_enable d = True
            Sets the class attribute remote_device_e nabled to a useful value at last.

            ---my main.py file:
            >
            import from myGlobals import *
            Gives a Syntax error.

            If you're not going to be bothered to test the code before you send it,
            I'm not sure I can be bothered to debug it for you.

            RDE = myGlobals.remot e_device_enable d
            Creates a local name RDE which takes its initial value from the class
            attribute remote_device_e nabled. This could be more easily written as:

            RDE = bool

            since initialize() hasn't been called yet.

            def main():
            if RDE: # this will not give me the correct value
            process_device( )
            Obvious something else is happening in your real code, because following
            the program logic you've shown (ignoring the SyntaxError), it should give
            the correct value: RDE is bool. By a lucky accident, "if bool" evaluates
            as True and process_device( ) will be called.

            I suggest that your spaghetti code is far more complicated and you
            haven't successfully teased out a minimal thread that demonstrates the
            problem.


            --
            Steven

            Comment

            • Bruno Desthuilliers

              #7
              Re: Question about scope

              Pat a écrit :
              (snip)
              >
              Stripping out the extra variables and definitions, this is all that
              there is.
              Whether or not this technique is *correct* programming is irrelevant.
              It's obviously relevant. If it was correct, it would work, and you
              wouldn't be asking here !-)
              I
              simply want to know why scoping doesn't work like I thought it would.
              >
              >
              ---myGlobals.py file:
              >
              class myGlobals():
              remote_device_e nabled = bool
              <irrelevant>
              You're using the class as a bare namespace. FWIW, you could as well use
              the module itself - same effect, simplest code.
              </irrelevant>
              ---my initialize.py file:
              >
              from myGlobals import *
              def initialize():
              myGlobals.remot e_device_enable d = True
              >
              ---my main.py file:
              >
              import from myGlobals import *
              I assume the first "import" is a typo. But this sure means you didn't
              run that code.
              RDE = myGlobals.remot e_device_enable d
              >
              def main():
              if RDE: # this will not give me the correct value
              For which definition of "correct value" ? You didn't import nor execute
              initialize() so far, so at this stage RDE is bound to the bool type
              object. FWIW, note that calling initialize *after* the assignement to
              RDE won't change the fact that RDE will be still bound to the the bool
              type object.

              <irrelevant>
              You may want to have a look at how other Python application manage
              application-wide settings.
              </irrelevant>

              Comment

              • Lawrence D'Oliveiro

                #8
                Re: Question about scope

                In message <0110f0fd$0$206 57$c3e8da3@news .astraweb.com>, Steven D'Aprano
                wrote:
                Why is it a class attribute instead of an instance attribute?
                Singleton class.

                Comment

                • Kirk Strauser

                  #9
                  Re: Question about scope

                  At 2008-10-24T01:08:12Z, Pat <Pat@junk.netwr ites:
                  ---myGlobals.py file:
                  >
                  class myGlobals():
                  remote_device_e nabled = bool
                  >
                  ---my initialize.py file:
                  >
                  from myGlobals import *
                  def initialize():
                  myGlobals.remot e_device_enable d = True
                  >
                  ---my main.py file:
                  >
                  import from myGlobals import *
                  RDE = myGlobals.remot e_device_enable d
                  >
                  def main():
                  if RDE: # this will not give me the correct value
                  process_device( )
                  If you *really* want to organize your settings like this, may I suggest:

                  ---myGlobals.py file:

                  # This does nothing more than create a placeholder
                  remote_device_e nabled = None

                  ---my initialize.py file:

                  import myGlobals
                  def initialize():
                  myGlobals.remot e_device_enable d = True

                  ---my main.py file:

                  import myGlobals
                  import initialize
                  initialize.init ialize()

                  def main():
                  if myGlobals.remot e_device_enable d:
                  process_device( )


                  --
                  Kirk Strauser
                  The Day Companies

                  Comment

                  • Pat

                    #10
                    Re: Question about scope

                    Steven D'Aprano wrote:
                    On Thu, 23 Oct 2008 11:38:35 -0400, Pat wrote:
                    >
                    >I have a Globals class.
                    >
                    Well, that's your first mistake. Using global variables in a class is no
                    better than using bare global variables. They're still global, and that's
                    a problem:
                    >

                    >
                    >
                    It depends upon the situation.

                    In my program, I have one routine that loads a bunch of files and
                    initializes a number of variables. After that, the values in the
                    globals class never change. It's a lot easier to maintain this type of
                    code than to passing the same variables from function to function to
                    function.

                    On the other hand, if multiple functions were willy-nilly changing
                    global variables then globals would be a maintenance nightmare.

                    To unilaterally state that globals are always "evil" borders on a
                    subjective religious conviction.

                    Comment

                    Working...