1

所以,首先这是我的代码:

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: global name 'setup' is not defined”。

有没有人有任何想法或者他们可以向我解释这个?

山姆

4

2 回答 2

2

setup是您的Thread实例的方法。因此,您使用self.setup()而不是调用它setup()。后者试图调用一个setup不存在的名为的全局函数。

由于setup()是一个实例方法,它也必须接受self作为它的第一个参数。

于 2013-01-23T17:54:03.520 回答
2

我假设您打算执行以下操作:

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'
于 2013-01-23T17:54:12.443 回答