使用Thread类创建线程函数传参无法传递类对象的问题
如题import threading
def testfunc(class_name):
class_name.class_test()
class Test_A():
def __init__(self):
pass
def class_test(self):
print("test")
a = Test_A()
test_thread = threading.Thread(target = testfunc, args = (a))
test_thread.start()
这段代码运行会报错:TypeError: testfunc() argument after * must be an iterable, not Test_A
如果我要传递一个类对象作为参数,我应该放弃这种创建线程函数的方法吗? 如果元组中只有一个元素,那么末尾要加逗号
import threading
def testfunc(class_name):
class_name.class_test()
class Test_A():
def __init__(self):
pass
def class_test(self):
print("test")
a = Test_A()
test_thread = threading.Thread(target = testfunc, args = (a,))
test_thread.start()
页:
[1]