How to declare a LIST as GLOBAL in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • getmanup
    New Member
    • May 2010
    • 4

    How to declare a LIST as GLOBAL in python

    Can we declare using th keyword "global" in declaring a global array?

    ie like global arr = []

    This throws an error saying invalid syntax.
    Last edited by Niheel; May 21 '10, 04:19 AM. Reason: punctuation, grammar,
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Not enough info. What is the code and what is the error message. Finally, you should not be using globals. Pass the list (not array) to the function and return it, or better still, use a class. A link to a book describing function args in case you don't know http://www.greenteapress.com/thinkpy...l/book007.html

    Comment

    • getmanup
      New Member
      • May 2010
      • 4

      #3
      I declared a list as I had to append some threads into it.
      Code:
      global th = []
      th.append(thread)
      These 2 lines of code were written in one function. In another function I want to join them like:
      Code:
      for t in th
           t.join()
      But this code throws an error telling that global th= [] is invalid syntax.
      Last edited by Niheel; May 21 '10, 04:21 AM. Reason: Please read guidelines on how to post questions and answers on Bytes. Punctuation and grammar were fixed. Code tags applied

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Originally posted by getmanup
        I declared a list as I had to append some threads into it.
        Code:
        global th = []
        th.append(thread)
        These 2 lines of code were written in one function. In another function I want to join them like:
        Code:
        for t in th
             t.join()
        But this code throws an error telling that global th= [] is invalid syntax.
        When you say threads do you mean this kind of thread? I'm pretty sure that joining them is not valid. So what @dwblas was saying was that you should rather pass your "th" variable across explicitly.

        Code:
        def firstcode():
             ...
            th=[]
            th.append(thread)
            ...
            return th
        
        def nextmethod(th):
            for t in th:
                t.join()
        But if you really want to make th global, just add
        Code:
        global th
        th=[]
        to your code.

        Comment

        • getmanup
          New Member
          • May 2010
          • 4

          #5
          Originally posted by Glenton
          When you say threads do you mean this kind of thread? I'm pretty sure that joining them is not valid. So what @dwblas was saying was that you should rather pass your "th" variable across explicitly.

          Code:
          def firstcode():
               ...
              th=[]
              th.append(thread)
              ...
              return th
          
          def nextmethod(th):
              for t in th:
                  t.join()
          But if you really want to make th global, just add
          Code:
          global th
          th=[]
          to your code.
          thank you so much... it worked

          but u did say that joining threads like that is not a good idea...I am really in the beginner level...so could you explain why you said so..if its ok for u ...

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Um...I don't think you're using threads in the coding sense. Probably it's just a string, so don't worry about it. If you're not sure, then you're definitely not using them!

            Comment

            Working...