我很好奇,爲什麼當我的類繼承Thread時,我不能調用super(Thread, self).__init__()
而不是Thread.__init__(self)
。你能幫我理解這個問題嗎?當從線程繼承時調用超類構造函數
#!/usr/bin/python
from threading import Thread
from Queue import Queue
class ThreadManager(object):
def work(self, items):
q = Queue()
for item in items:
q.put(item)
print q.qsize()
p = Worker()
p.start()
p.join()
class Worker(Thread):
def __init__(self):
# Why doesn't this work?
#super(Thread, self).__init__()
Thread.__init__(self)
def run(self):
print 'thread running'
def main():
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
tm = ThreadManager()
tm.work(items)
if __name__ == "__main__":
main()
所有例子字母的類名,所以我糊塗了!謝謝你的回答! – 2013-03-01 02:01:33
@DavidM.Coe:這是一個很好的觀點......它應該提醒我們其他人停止編寫例子,其中A從B繼承,反之亦然,並提出實際名稱...... – abarnert 2013-03-01 02:10:41