Python 使用类构造函数作为函数创建线程
我知道我可以使用以下语法在Python中创建和启动线程:Python 使用类构造函数作为函数创建线程,python,multithreading,class,constructor,Python,Multithreading,Class,Constructor,我知道我可以使用以下语法在Python中创建和启动线程: import threading tracker_thread = threading.Thread(target=func, args=(x,y,z)) tracker_thread.daemon = True tracker_thread.start() 我需要的是使用类构造函数作为target。我知道解决方案如下: def wrap_function(x,y,z): MyClas(x,y,z) tracker_thre
import threading
tracker_thread = threading.Thread(target=func, args=(x,y,z))
tracker_thread.daemon = True
tracker_thread.start()
我需要的是使用类构造函数作为target
。我知道解决方案如下:
def wrap_function(x,y,z):
MyClas(x,y,z)
tracker_thread = threading.Thread(target=wrap_function, args=(x,y,z))
但是我试图通过在线程
的初始化中只使用构造函数来避免使用类似的“wrap函数”
我该怎么做?已解决。最简单的解决方案总是最好的:
tracker_thread = threading.Thread(target=MyClass, args=(x,y,z))
感谢您的评论。
threading.Thread(target=MyClass,args=(x,y,z))
不起作用吗?是的,它起作用了。。。谢谢,是args=[x,y,z]还是args=(x,y,z)?是的——Python“没有用于实例化对象的特殊语法”再次获胜!函数/类交换给我的唯一一次机会是将decorator作为类编写,并使用它在Python3中修饰方法。(提示:您的装饰程序将需要一个\uuuu获取\uuuu
方法)