[英]Run a class-method every n seconds
我尝试每n秒在Python 3中运行一个类方法。
我认为线程化将是一个很好的方法。 问题( 每n秒运行某些代码 )显示了如何在没有对象的情况下执行此操作。
我试图像这样将代码“转移”到OOP:
class Test:
import threading
def printit():
print("hello world")
threading.Timer(5.0, self.printit).start()
test = Test()
test.printit()
>> TypeError: printit() takes no arguments (1 given)
我得到这个错误。
你能帮我做对吗?
将参数self添加到printit方法中,它对我有用。 同样,import语句应位于文件的顶部,而不是在类定义之内。
import threading
class Test:
def printit(self):
print("hello world")
threading.Timer(5.0, self.printit).start()
test = Test()
test.printit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.