1

NameError: global name 'import_1' is not definedはこのガイドを使用しています:

http://www.ibiblio.org/g2swap/byteofpython/read/class-and-object-vars.html

コードは次のとおりです。

class test_imports:#Test classes remove 
      alive = {'import_1': True, 'import_2': True};

      def halt_listener(self, control_Queue, thread_Name, kill_command):
          global alive
          while True:
              isAlive = control_queue.get()
              if isAlive == kill_command:
                 test_imports.alive[thread_Name] = False;
                 return

      def import_1(self, control_Queue, thread_Number):
          print ("Import_1 number %d started") % thread_Number
          t = Thread(target=test_imports.halt_listener, args=(control_Queue, 'import_1', 't1kill'))
          count = 0 
          global alive 
          run = test_imports.alive[import_1];
          while run:
                print ("Thread type 1 number %d run count %d") % (thread_number, count)
                count = count + 1
                print ("Test Import_1 ", run)
                run = test_Imports.alive['import_1'];
          print ("Killing thread type 1 number %d") % thread_Number 

私は何が欠けていますか?みんなありがとう!

4

1 に答える 1

4

あなたは引用符を忘れました:

run = test_imports.alive['import_1']

クラス内では を使用する必要がないことに注意してください。次に、インスタンス属性だけでなく、クラスを参照するためにglobal使用できるメソッド内でも使用できます。self

run = self.alive['import_1']
于 2013-08-12T00:18:36.223 に答える