【发布时间】:2017-04-14 23:32:57
【问题描述】:
下面的程序只打印一次 hello world 而不是每 5 秒打印一次字符串。
from threading import Timer;
class TestTimer:
def __init__(self):
self.t1 = Timer(5.0, self.foo);
def startTimer(self):
self.t1.start();
def foo(self):
print("Hello, World!!!");
timer = TestTimer();
timer.startTimer();
(program - 1)
但下面的程序每 5 秒打印一次字符串。
def foo():
print("World");
Timer(5.0, foo).start();
foo();
(program - 2)
为什么 (program - 1) 不是每 5 秒打印一次字符串?以及如何使(程序-1)连续每5秒打印一次字符串。
【问题讨论】:
-
你为什么要把它包装在一个额外的类中?有必要吗?
标签: python-3.x