繁体   English   中英

如何在python中引用类变量

[英]How to reference a class variable in python

我似乎无法让类读取它继续NameError: global name 'import_1' is not defined的活字典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 

我错过了什么? 多谢你们!

你忘了引号:

run = test_imports.alive['import_1']

请注意,在class内部不需要使用global ,其次是内部方法,您可以使用self来引用类以及实例属性:

run = self.alive['import_1']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM