[英]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.