Access to variable from external imported module

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

    Access to variable from external imported module

    How to access to a variable (that value is not returned) from a module
    imported?
    And the variable is set at the module-level.

    That module is external to my program, it's from another project so I
    wann't modifying it.

  • Fredrik Lundh

    #2
    Re: Access to variable from external imported module

    GinTon wrote:
    How to access to a variable (that value is not returned) from a module
    imported?
    And the variable is set at the module-level.
    import module
    print module.variable

    (have you read the Python tutorial?)

    </F>

    Comment

    • GinTon

      #3
      Re: Access to variable from external imported module

      Sorry, I mean access to local variable from a method

      import module
      method(value)

      I would to access to values that are created locally in that method

      Fredrik Lundh ha escrito:
      GinTon wrote:
      >
      How to access to a variable (that value is not returned) from a module
      imported?
      And the variable is set at the module-level.
      >
      import module
      print module.variable
      >
      (have you read the Python tutorial?)
      >
      </F>

      Comment

      • robert

        #4
        Re: Access to variable from external imported module

        GinTon wrote:
        Sorry, I mean access to local variable from a method
        >
        import module
        method(value)
        >
        I would to access to values that are created locally in that method
        after the method has executed? usually the return value?
        or you want to get all local variables, then make a func/method

        def f(a=1):
        b=2
        c=3
        return locals() #X/Object(locals() )


        --------


        d=module.f()
        print d['c'] # d.c





        Robert

        Comment

        • Bjoern Schliessmann

          #5
          Re: Access to variable from external imported module

          GinTon wrote:
          Sorry, I mean access to local variable from a method
          >
          import module
          method(value)
          That's no access to a local variable of a method. It's a simple
          function call.
          I would to access to values that are created locally in that
          method
          Something with your interface seems horribly wrong.

          Regards,


          Björn

          --
          BOFH excuse #316:

          Elves on strike. (Why do they call EMAG Elf Magic)

          Comment

          • Bruno Desthuilliers

            #6
            Re: Access to variable from external imported module

            GinTon a écrit :
            Sorry, I mean access to local variable from a method
            One of the most surprising properties of local variables is that they
            are, well... local.

            Comment

            • jim-on-linux

              #7
              Re: Access to variable from external imported module



              GinTon,

              I think this is what you want.


              class Kdoi:
                 def __init__(self) :    
                     self.Fdo()

                 def Fdo(self):

              searchterm = 'help'
              print searchterm #local

                   self.searchterm = searchterm
                   print self.searchterm #used inside the class

                   Kdo.searchterm = searchterm #<<<<
                   print Kdo.searchterm #used outside the class
              Kdomore()


                 
              class Kdomore(Kdo):
              def __init__(self) :
              self.Fdomore()

              def Fdomore(self):
              searchterm = Kdo.searchterm # <<<<
              print searchterm



              jim-on-linux









              On Thursday 23 November 2006 17:09, GinTon wrote:
              Sorry, I mean access to local variable from a
              method
              >
              import module
              method(value)
              >
              I would to access to values that are created
              locally in that method
              >
              Fredrik Lundh ha escrito:
              GinTon wrote:
              How to access to a variable (that value is
              not returned) from a module imported?
              And the variable is set at the
              module-level.
              import module
              print module.variable

              (have you read the Python tutorial?)

              </F>

              Comment

              • GinTon

                #8
                Re: Access to variable from external imported module

                Thanks Robert, the best solution is get all local variables, else is
                impossible access to them.

                robert ha escrito:
                GinTon wrote:
                I would to access to values that are created locally in that method
                >
                after the method has executed? usually the return value?
                or you want to get all local variables, then make a func/method
                >
                def f(a=1):
                b=2
                c=3
                return locals() #X/Object(locals() )
                >
                --------
                >
                d=module.f()
                print d['c'] # d.c

                Comment

                • Fredrik Lundh

                  #9
                  Re: Access to variable from external imported module

                  GinTon wrote:
                  Thanks Robert, the best solution is get all local variables, else is
                  impossible access to them.
                  if you don't want them to be local, why are you using local variables?

                  (have you read the Python tutorial?)

                  </F>

                  Comment

                  • John Machin

                    #10
                    Re: Access to variable from external imported module


                    jim-on-linux wrote:
                    GinTon,
                    >
                    I think this is what you want.
                    >
                    >
                    class Kdoi:
                    Is that a typo?
                    def __init__(self) :
                    self.Fdo()
                    >
                    What is all this K and F stuff?
                    def Fdo(self):
                    >
                    searchterm = 'help'
                    print searchterm #local
                    >
                    self.searchterm = searchterm
                    print self.searchterm #used inside the class
                    >
                    Kdo.searchterm = searchterm #<<<<
                    print Kdo.searchterm #used outside the class
                    Kdomore()
                    >
                    >
                    >
                    class Kdomore(Kdo):
                    def __init__(self) :
                    self.Fdomore()
                    >
                    def Fdomore(self):
                    searchterm = Kdo.searchterm # <<<<
                    print searchterm
                    It's not apparent what the print statements are for -- are they part of
                    an attempt to debug your code?

                    What gives you the idea that this is what the OP wants or needs?

                    Comment

                    • robert

                      #11
                      Re: Access to variable from external imported module

                      GinTon wrote:
                      Thanks Robert, the best solution is get all local variables, else is
                      impossible access to them.
                      For test purposes/ex post inspection you could also uncomment the line in:

                      def f(a=1):
                      b=2
                      c=3
                      #globals().upda te(locals())
                      return a+b
                      --

                      then it is more easy and you can get it like:

                      module.c


                      You can also create a total stack trace dynamically with this trick function:

                      def mktb():
                      try: raise UserWarning
                      except: return sys.exc_info()[2]

                      def f(a=1):
                      b=2
                      c=3
                      global ftb;ftb=mktb()
                      return a+b
                      ----

                      and then fully inspect the total situation in the func (and all down the call history) ex post at any time with
                      >>f()
                      >>pdb.post_mort em(module.ftb) # then do once 'up' in pdb/pywin.debugger. ..
                      >>pywin.debugge r.post_mortem(m odule.ftb)

                      Which other programming language can do things like this?
                      ( Unfortunately (legacy) Python has no possibility to (re-)continue execution from exceptions/traces other than by simple generators )

                      Robert


                      robert ha escrito:
                      >GinTon wrote:
                      >>I would to access to values that are created locally in that method
                      >after the method has executed? usually the return value?
                      >or you want to get all local variables, then make a func/method
                      >>
                      >def f(a=1):
                      > b=2
                      > c=3
                      > return locals() #X/Object(locals() )
                      >>
                      >--------
                      >>
                      >d=module.f()
                      >print d['c'] # d.c
                      >

                      Comment

                      • jim-on-linux

                        #12
                        Re: Access to variable from external imported module







                        On Friday 24 November 2006 03:30, John Machin
                        wrote:
                        jim-on-linux wrote:
                        GinTon,

                        I think this is what you want.


                        class Kdoi:
                        >
                        Is that a typo?
                        No, it's a style. life seems to be easier
                        to me if one is consistent, all my classes begin
                        with K.
                        >
                        def __init__(self) :
                        self.Fdo()
                        >
                        What is all this K and F stuff?
                        >
                        It's my style. life seems to be easier to me
                        if one is consistent all my function begin with
                        F.

                        I started doing things like this when the only way
                        to debug was to read each line of code and try to
                        figgure out if it was the problem.
                        They are my personal sign posts.
                        def Fdo(self):
                        searchterm = 'help'
                        print searchterm #local

                        self.searchterm = searchterm
                        print self.searchterm #used inside the
                        class

                        Kdo.searchterm = searchterm #<<<<
                        print Kdo.searchterm #used outside the
                        class Kdomore()



                        class Kdomore(Kdo):
                        def __init__(self) :
                        self.Fdomore()

                        def Fdomore(self):
                        searchterm = Kdo.searchterm #
                        <<<< print searchterm
                        >
                        It's not apparent what the print statements are
                        for -- are they part of an attempt to debug
                        your code?
                        >
                        print shows the results wherever a print statement
                        turns up the results = 'help' .
                        I didn't run the code, and it has it has a coding
                        error but if removed, the results should be;

                        searchterm = 'help'
                        self.searchterm = 'help'
                        Kdo.searchterm = 'help'

                        Sound silly but many people have trouble with
                        getting a variable from here to there in their
                        code. This shows that it can be done
                        What gives you the idea that this is what the
                        OP wants or needs?
                        If I remember right, he refrased his first
                        question and asked a second one.
                        Sometimes people don't take the time to write
                        correctly, the questions that are really in their
                        mind. So I guessed. If Im wrong, he will ignore
                        it. If I'm right, he will use it.

                        Also, I have found that other will latch on to the
                        ideas presented in these email responses. And
                        they will use them, even though the response was
                        not exactly what the original emailer wanted.

                        And, I sometimes I do use print statements to
                        debug, I have used other ways but on linux, I
                        prefer a print statement.

                        jim-on-linux












                        Comment

                        • jim-on-linux

                          #13
                          Re: Access to variable from external imported module

                          On Friday 24 November 2006 13:01, jim-on-linux
                          wrote:
                          On Friday 24 November 2006 03:30, John Machin
                          >
                          wrote:
                          jim-on-linux wrote:
                          GinTon,
                          >
                          I think this is what you want.
                          >
                          >
                          class Kdoi:
                          Is that a typo?
                          >
                          No, it's a style. life seems to be
                          easier to me if one is consistent, all my
                          classes begin with K.
                          Sorry, Kdoi should be Kod


                          >
                          def __init__(self) :
                          self.Fdo()
                          What is all this K and F stuff?
                          >
                          It's my style. life seems to be easier to
                          me if one is consistent all my function begin
                          with F.
                          >
                          I started doing things like this when the only
                          way to debug was to read each line of code and
                          try to figgure out if it was the problem.
                          They are my personal sign posts.
                          >
                          def Fdo(self):
                          >
                          >
                          searchterm = 'help'
                          print searchterm #local
                          >
                          self.searchterm = searchterm
                          print self.searchterm #used inside the
                          class
                          >
                          Kdo.searchterm = searchterm #<<<<
                          print Kdo.searchterm #used outside the
                          class Kdomore()
                          the line above should be Kdomore(), not class
                          Kdomore() (For the technocrats)
                          >
                          >
                          >
                          class Kdomore(Kdo):
                          def __init__(self) :
                          self.Fdomore()
                          >
                          def Fdomore(self):
                          searchterm = Kdo.searchterm #
                          <<<< print searchterm
                          It's not apparent what the print statements
                          are for -- are they part of an attempt to
                          debug your code?
                          >
                          print shows the results wherever a print
                          statement turns up the results = 'help' .
                          I didn't run the code, and it has it has a
                          coding error but if removed, the results should
                          be;
                          >
                          searchterm = 'help'
                          self.searchterm = 'help'
                          Kdo.searchterm = 'help'
                          >
                          Sound silly but many people have trouble
                          with getting a variable from here to there in
                          their code. This shows that it can be done
                          >
                          What gives you the idea that this is what the
                          OP wants or needs?
                          >
                          If I remember right, he refrased his first
                          question and asked a second one.
                          Sometimes people don't take the time to write
                          correctly, the questions that are really in
                          their mind. So I guessed. If Im wrong, he will
                          ignore it. If I'm right, he will use it.
                          >
                          Also, I have found that other will latch on to
                          the ideas presented in these email responses.
                          And they will use them, even though the
                          response was not exactly what the original
                          emailer wanted.
                          >
                          And, I sometimes I do use print statements to
                          debug, I have used other ways but on linux, I
                          prefer a print statement.
                          >
                          jim-on-linux

                          Comment

                          • jim-on-linux

                            #14
                            Re: Access to variable from external imported module

                            On Friday 24 November 2006 13:20, jim-on-linux
                            wrote:
                            On Friday 24 November 2006 13:01, jim-on-linux
                            >
                            wrote:
                            On Friday 24 November 2006 03:30, John Machin

                            wrote:
                            jim-on-linux wrote:
                            GinTon,

                            I think this is what you want.


                            class Kdoi:
                            >
                            Is that a typo?
                            No, it's a style. life seems to be
                            easier to me if one is consistent, all my
                            classes begin with K.
                            >
                            Sorry, Kdoi should be Kod
                            Sorry again Kdoi should be Kdo
                            (Haste makes waste.)
                            >
                            def __init__(self) :
                            self.Fdo()
                            >
                            What is all this K and F stuff?
                            It's my style. life seems to be easier to
                            me if one is consistent all my function begin
                            with F.

                            I started doing things like this when the
                            only way to debug was to read each line of
                            code and try to figgure out if it was the
                            problem. They are my personal sign posts.
                            def Fdo(self):


                            searchterm = 'help'
                            print searchterm #local

                            self.searchterm = searchterm
                            print self.searchterm #used inside
                            the class

                            Kdo.searchterm = searchterm #<<<<
                            print Kdo.searchterm #used outside
                            the class Kdomore()
                            >
                            the line above should be Kdomore(), not class
                            Kdomore() (For the technocrats)
                            >
                            class Kdomore(Kdo):
                            def __init__(self) :
                            self.Fdomore()

                            def Fdomore(self):
                            searchterm = Kdo.searchterm #
                            <<<< print searchterm
                            >
                            It's not apparent what the print statements
                            are for -- are they part of an attempt to
                            debug your code?
                            print shows the results wherever a print
                            statement turns up the results = 'help' .
                            I didn't run the code, and it has it has a
                            coding error but if removed, the results
                            should be;

                            searchterm = 'help'
                            self.searchterm = 'help'
                            Kdo.searchterm = 'help'

                            Sound silly but many people have trouble
                            with getting a variable from here to there in
                            their code. This shows that it can be done
                            What gives you the idea that this is what
                            the OP wants or needs?
                            If I remember right, he refrased his first
                            question and asked a second one.
                            Sometimes people don't take the time to write
                            correctly, the questions that are really in
                            their mind. So I guessed. If Im wrong, he
                            will ignore it. If I'm right, he will use
                            it.

                            Also, I have found that other will latch on
                            to the ideas presented in these email
                            responses. And they will use them, even
                            though the response was not exactly what the
                            original emailer wanted.

                            And, I sometimes I do use print statements to
                            debug, I have used other ways but on linux, I
                            prefer a print statement.
                            >
                            jim-on-linux

                            Comment

                            • John Machin

                              #15
                              Re: Access to variable from external imported module


                              jim-on-linux wrote:
                              On Friday 24 November 2006 03:30, John Machin
                              wrote:
                              jim-on-linux wrote:
                              GinTon,
                              >
                              I think this is what you want.
                              >
                              >
                              class Kdoi:
                              Is that a typo?
                              No, it's a style. life seems to be easier
                              to me if one is consistent, all my classes begin
                              with K.
                              and end with "i"?
                              def __init__(self) :
                              self.Fdo()
                              What is all this K and F stuff?
                              It's my style. life seems to be easier to me
                              if one is consistent all my function begin with
                              F.
                              You left out a word; the correct way of phrasing that is: "All my
                              function _are_ begin with F" :-)

                              This appears to be a variation on "Hungarian notation"; google that for
                              opinions pro & con.

                              In a certain vernacular, it would be called "an effed concept" :-)
                              >
                              I started doing things like this when the only way
                              to debug was to read each line of code and try to
                              figgure out if it was the problem.
                              When was that? Even years ago, there were slightly better ways. For
                              example, my first boss' boss was an enthusiastic coder and debugger and
                              also a workaholic. Colleagues who lived along the same railway line as
                              he and were foolish enough not to hide behind a newspaper could have
                              their morning or evening reverie disturbed by a cry of "Glad you're
                              here! I'll hold the listing, you hold the dump!". I get the impression
                              that debugging techniques have moved along a little bit since then. :-)
                              They are my personal sign posts.
                              def Fdo(self):
                              >
                              >
                              searchterm = 'help'
                              print searchterm #local
                              >
                              self.searchterm = searchterm
                              print self.searchterm #used inside the
                              class
                              >
                              Kdo.searchterm = searchterm #<<<<
                              print Kdo.searchterm #used outside the
                              class Kdomore()
                              >
                              >
                              >
                              class Kdomore(Kdo):
                              def __init__(self) :
                              self.Fdomore()
                              >
                              def Fdomore(self):
                              searchterm = Kdo.searchterm #
                              <<<< print searchterm
                              It's not apparent what the print statements are
                              for -- are they part of an attempt to debug
                              your code?
                              print shows the results wherever a print statement
                              turns up the results = 'help' .
                              I didn't run the code, and it has it has a coding
                              error
                              I noticed.
                              but if removed, the results should be;
                              >
                              searchterm = 'help'
                              self.searchterm = 'help'
                              Kdo.searchterm = 'help'
                              No, the result would be
                              help
                              help
                              help

                              Plug in a text-to-speech module and a phone dialer and you're done ;-)
                              >
                              Sound silly but many people have trouble with
                              getting a variable from here to there in their
                              code. This shows that it can be done
                              >
                              What gives you the idea that this is what the
                              OP wants or needs?
                              >
                              If I remember right, he refrased his first
                              question and asked a second one.
                              Sometimes people don't take the time to write
                              correctly, the questions that are really in their
                              mind. So I guessed. If Im wrong, he will ignore
                              it. If I'm right, he will use it.
                              With luck. Kindly consider another possibility: that you are wrong (or
                              just marching to the beat of your own tambourine) and he (or she) is a
                              newbie & will use it :-)

                              [snip]

                              HTH,
                              John

                              Comment

                              Working...