can a method access/set another's variables?

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

    #1

    can a method access/set another's variables?

    My code is:
    -a.py-
    import b

    class A:
    def __init__(self):
    pass
    def my_method(self) :
    var = 1
    self.var = 2
    b.set_var(self)
    print var
    print self.var

    my_a = A()
    my_a.my_method( )

    -b.py-
    def set_var(self):
    var = 2
    self.var = 2

    I want both var and self.var to be 2 at the end. Is there anything I
    can pass to set_var() that will give it access to the variables in
    my_method() like I can use self for the variables in the class A?
    Thanks!

  • Michael Hoffman

    #2
    Re: can a method access/set another's variables?

    asdf1234234 wrote:
    -a.py-
    import b
    >
    class A:
    def __init__(self):
    pass
    def my_method(self) :
    var = 1
    self.var = 2
    b.set_var(self)
    print var
    print self.var
    >
    my_a = A()
    my_a.my_method( )
    >
    -b.py-
    def set_var(self):
    var = 2
    self.var = 2
    >
    I want both var and self.var to be 2 at the end. Is there anything I
    can pass to set_var() that will give it access to the variables in
    my_method() like I can use self for the variables in the class A?
    I hope there isn't a way to do this that simply. :) Why do you want to
    do this, or is it idle curiosity? There is almost surely a better way to
    solve your underlying problem.

    You can *read* your caller's local variables (either pass locals() as an
    argument or use inspect to get the frame locals), but writing to this
    dictionary has undefined behavior.
    --
    Michael Hoffman

    Comment

    • wswilson

      #3
      Re: can a method access/set another's variables?

      On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
      asdf1234234 wrote:
      -a.py-
      import b
      >
      class A:
      def __init__(self):
      pass
      def my_method(self) :
      var = 1
      self.var = 2
      b.set_var(self)
      print var
      print self.var
      >
      my_a = A()
      my_a.my_method( )
      >
      -b.py-
      def set_var(self):
      var = 2
      self.var = 2
      >
      I want both var and self.var to be 2 at the end. Is there anything I
      can pass to set_var() that will give it access to the variables in
      my_method() like I can use self for the variables in the class A?
      >
      I hope there isn't a way to do this that simply. :) Why do you want to
      do this, or is it idle curiosity? There is almost surely a better way to
      solve your underlying problem.
      >
      You can *read* your caller's local variables (either pass locals() as an
      argument or use inspect to get the frame locals), but writing to this
      dictionary has undefined behavior.
      --
      Michael Hoffman
      I am parsing a document which contains some lines with code I want to
      eval or exec. However, due to the complexity of the parsing, I am
      splitting it among different methods. So, if I eval something in one
      method, it won't be there if I try to access its value a few lines
      later since I happen to be in a different method in the parser. Thanks
      for the help!

      Comment

      • 7stud

        #4
        Re: can a method access/set another's variables?

        On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail .comwrote:
        On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
        >
        >
        >
        asdf1234234 wrote:
        -a.py-
        import b
        >
        class A:
        def __init__(self):
        pass
        def my_method(self) :
        var = 1
        self.var = 2
        b.set_var(self)
        print var
        print self.var
        >
        my_a = A()
        my_a.my_method( )
        >
        -b.py-
        def set_var(self):
        var = 2
        self.var = 2
        >
        I want both var and self.var to be 2 at the end. Is there anything I
        can pass to set_var() that will give it access to the variables in
        my_method() like I can use self for the variables in the class A?
        >
        I hope there isn't a way to do this that simply. :) Why do you want to
        do this, or is it idle curiosity? There is almost surely a better way to
        solve your underlying problem.
        >
        You can *read* your caller's local variables (either pass locals() as an
        argument or use inspect to get the frame locals), but writing to this
        dictionary has undefined behavior.
        --
        Michael Hoffman
        >
        I am parsing a document which contains some lines with code I want to
        eval or exec. However, due to the complexity of the parsing, I am
        splitting it among different methods. So, if I eval something in one
        method, it won't be there if I try to access its value a few lines
        later since I happen to be in a different method in the parser. Thanks
        for the help!
        class Parser(object):
        def early_parse(sel f):
        self.result1 = eval("10 + 5")

        def later_parse(sel f):
        self.result2 = self.result1 + eval("20 * 2")

        p = Parser()
        p.early_parse()
        p.later_parse()
        print p.result1
        print p.result2

        Comment

        • 7stud

          #5
          Re: can a method access/set another's variables?

          On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail .comwrote:
          On Apr 1, 9:43 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
          >
          >
          >
          asdf1234234 wrote:
          -a.py-
          import b
          >
          class A:
          def __init__(self):
          pass
          def my_method(self) :
          var = 1
          self.var = 2
          b.set_var(self)
          print var
          print self.var
          >
          my_a = A()
          my_a.my_method( )
          >
          -b.py-
          def set_var(self):
          var = 2
          self.var = 2
          >
          I want both var and self.var to be 2 at the end. Is there anything I
          can pass to set_var() that will give it access to the variables in
          my_method() like I can use self for the variables in the class A?
          >
          I hope there isn't a way to do this that simply. :) Why do you want to
          do this, or is it idle curiosity? There is almost surely a better way to
          solve your underlying problem.
          >
          You can *read* your caller's local variables (either pass locals() as an
          argument or use inspect to get the frame locals), but writing to this
          dictionary has undefined behavior.
          --
          Michael Hoffman
          >
          I am parsing a document which contains some lines with code I want to
          eval or exec. However, due to the complexity of the parsing, I am
          splitting it among different methods. So, if I eval something in one
          method, it won't be there if I try to access its value a few lines
          later since I happen to be in a different method in the parser. Thanks
          for the help!
          class A(object):
          def early_parse(sel f):
          self.result1 = eval("10 + 5")

          class MyParser(object ):
          def later_parse(sel f):
          MyParser.a.resu lt2 = MyParser.a.resu lt1 + eval("20 *
          2")

          a = A()

          p = MyParser()
          p.a.early_parse ()
          p.later_parse()
          print p.a.result1
          print p.a.result2

          Comment

          • 7stud

            #6
            Re: can a method access/set another's variables?

            On Apr 1, 7:43 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
            asdf1234234 wrote:
            -a.py-
            import b
            >
            class A:
            def __init__(self):
            pass
            def my_method(self) :
            var = 1
            self.var = 2
            b.set_var(self)
            print var
            print self.var
            >
            my_a = A()
            my_a.my_method( )
            >
            -b.py-
            def set_var(self):
            var = 2
            self.var = 2
            >
            I want both var and self.var to be 2 at the end. Is there anything I
            can pass to set_var() that will give it access to the variables in
            my_method() like I can use self for the variables in the class A?
            >
            I hope there isn't a way to do this that simply. :) Why do you want to
            do this, or is it idle curiosity? There is almost surely a better way to
            solve your underlying problem.
            >
            You can *read* your caller's local variables (either pass locals() as an
            argument or use inspect to get the frame locals), but writing to this
            dictionary has undefined behavior.
            --
            Michael Hoffman
            class A(object):
            def early_parse(sel f):
            self.result1 = eval("10+5")
            def later_parse(A_o bj):
            A_obj.result2 = eval("20*2")

            a = A()
            a.early_parse()
            later_parse(a)
            print a.result1
            print a.result2

            Comment

            • 7stud

              #7
              Re: can a method access/set another's variables?

              On Apr 1, 9:24 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
              On Apr 1, 7:43 pm, Michael Hoffman <cam.ac...@mh39 1.invalidwrote:
              >
              >
              >
              asdf1234234 wrote:
              -a.py-
              import b
              >
              class A:
              def __init__(self):
              pass
              def my_method(self) :
              var = 1
              self.var = 2
              b.set_var(self)
              print var
              print self.var
              >
              my_a = A()
              my_a.my_method( )
              >
              -b.py-
              def set_var(self):
              var = 2
              self.var = 2
              >
              I want both var and self.var to be 2 at the end. Is there anything I
              can pass to set_var() that will give it access to the variables in
              my_method() like I can use self for the variables in the class A?
              >
              I hope there isn't a way to do this that simply. :) Why do you want to
              do this, or is it idle curiosity? There is almost surely a better way to
              solve your underlying problem.
              >
              You can *read* your caller's local variables (either pass locals() as an
              argument or use inspect to get the frame locals), but writing to this
              dictionary has undefined behavior.
              --
              Michael Hoffman
              >
              class A(object):
              def early_parse(sel f):
              self.result1 = eval("10+5")
              def later_parse(A_o bj):
              A_obj.result2 = eval("20*2")
              >
              a = A()
              a.early_parse()
              later_parse(a)
              print a.result1
              print a.result2
              Oops. That def should be:

              def later_parse(A_o bj):
              A_obj.result2 = A_obj.result1 + eval("20*2")


              Comment

              • 7stud

                #8
                Re: can a method access/set another's variables?

                On Apr 1, 7:56 pm, "wswilson" <wswil...@gmail .comwrote:
                I am parsing a document which contains some lines with code I want to
                eval or exec. However, due to the complexity of the parsing, I am
                splitting it among different methods. So, if I eval something in one
                method, it won't be there if I try to access its value a few lines
                later since I happen to be in a different method in the parser. Thanks
                for the help!
                val = None
                class A(object):
                def early_parse(sel f):
                global val
                self.result1 = val = eval("10 + 5")
                def later_parse():
                global val
                val += eval("20*2")

                a = A()
                a.early_parse()
                later_parse()
                a.result2 = val
                print a.result1
                print a.result2

                Comment

                • Alex Martelli

                  #9
                  Re: can a method access/set another's variables?

                  asdf1234234 <wswilson@gmail .comwrote:
                  My code is:
                  -a.py-
                  import b
                  >
                  class A:
                  def __init__(self):
                  pass
                  Incidentally, these last two lines are totally, utterly useless. Do NOT
                  define special methods like this -- just omit the whole def statement
                  and you'll have identical semantics, no wasted boilerplate, even better
                  performance.


                  Alex

                  Comment

                  • wswilson

                    #10
                    Re: can a method access/set another's variables?

                    On Apr 2, 1:04 am, a...@mac.com (Alex Martelli) wrote:
                    asdf1234234 <wswil...@gmail .comwrote:
                    My code is:
                    -a.py-
                    import b
                    >
                    class A:
                    def __init__(self):
                    pass
                    >
                    Incidentally, these last two lines are totally, utterly useless. Do NOT
                    define special methods like this -- just omit the whole def statement
                    and you'll have identical semantics, no wasted boilerplate, even better
                    performance.
                    >
                    Alex
                    I know they're useless. I was using the init method before and just
                    replaced what I had with pass instead of deleting it because I
                    expected to use it again later. Thanks for the tip, though.

                    Comment

                    • wswilson

                      #11
                      Re: can a method access/set another's variables?

                      On Apr 2, 11:01 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
                      On 1 Apr 2007 18:36:04 -0700, "asdf123423 4" <wswil...@gmail .com>
                      declaimed the following in comp.lang.pytho n:
                      >
                      My code is:
                      -a.py-
                      import b
                      >
                      class A:
                      def __init__(self):
                      pass
                      >
                      Delete this empty __init__() [and, consider converting the class to
                      new-style]
                      >
                      def my_method(self) :
                      var = 1
                      >
                      var is a local to method only name, it goes away when the method
                      ends.
                      >
                      self.var = 2
                      b.set_var(self)
                      print var
                      print self.var
                      >
                      my_a = A()
                      my_a.my_method( )
                      >
                      -b.py-
                      def set_var(self):
                      var = 2
                      >
                      Again, var is local to just set_var().
                      >
                      self.var = 2
                      >
                      I want both var and self.var to be 2 at the end. Is there anything I
                      can pass to set_var() that will give it access to the variables in
                      my_method() like I can use self for the variables in the class A?
                      >
                      The common scheme, unless you expect to have multiple instances of
                      this class with /separate/ local "var", would be to put var in a
                      "common" module
                      >
                      -=-=-=-=- common.py
                      >
                      var = None
                      >
                      -=-=-=-=-
                      >
                      Then have both a.py and b.py "import common", followed by changing
                      all "var =" to "common.var ="
                      --
                      Wulfraed Dennis Lee Bieber KD6MOG
                      wlfr...@ix.netc om.com wulfr...@bestia ria.com

                      (Bestiaria Support Staff: web-a...@bestiaria. com)
                      HTTP://www.bestiaria.com/
                      Thanks! I like the sound of that...it should work for what I need it
                      to do.

                      Comment

                      Working...