[英]Python - Function isn't 'Global' and so can't be called within Threading Class
因此,首先是我的代码:
import threading
print "Press Escape to Quit"
class threadOne(threading.Thread): #I don't understand this or the next line
def run(self):
setup()
def setup():
print 'hello world - this is threadOne'
class threadTwo(threading.Thread):
def run(self):
print 'ran'
threadOne().start()
threadTwo().start()
因此,问题在于我的类“ threadOne”中运行了运行函数(由线程模块调用),但是从那里我无法调用任何其他函数。 这包括是否要在setup()函数下创建更多函数。 例如,在上面的示例中,我在run(self)函数中尝试调用setup()并获取“ NameError:全局名称'setup'未定义”。
有人有任何想法或可以向我解释吗?
山姆
setup
是您的Thread
实例的一种方法。 因此,您可以使用self.setup()
而不是setup()
调用它。 后者正在尝试调用不存在的名为setup
的全局函数。
由于setup()
是实例方法,因此它也必须接受self
作为其第一个参数。
我认为您打算执行以下操作:
class threadOne(threading.Thread): #I don't understand this or the next line
def run(self):
self.setup()
def setup(self):
print 'hello world - this is threadOne'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.