So, first off here’s my code:
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()
So, the problem is that within my class ‘threadOne’ the run function runs (as that is called by the threading module) but from there I can not called any other functions. That includes if I make more functions beneath the setup() function. For example above, in my run(self) function I try and call setup() and get ‘NameError: global name ‘setup’ is not defined’.
Does anybody have any ideas or can they explain this to me?
Sam
setupis a method of yourThreadinstance. Therefore, you call it withself.setup()rather thansetup(). The latter is trying to call a global function namedsetupwhich does not exist.Since
setup()is an instance method, it must acceptselfas its first parameter as well.