如何让这个类在python中运行?
我猜想如何让这个类在python中运行?,python,class,object,show,Python,Class,Object,Show,我猜想sleep()函数来自time库。加上 class script(object): def __init__(self, time_delay = 1.5): self.time_delay = time_delay self.lines = [] def run_script(self): for line in self.lines: print line sleep(s
sleep()
函数来自time
库。加上
class script(object):
def __init__(self, time_delay = 1.5):
self.time_delay = time_delay
self.lines = []
def run_script(self):
for line in self.lines:
print line
sleep(self.time_delay)
intro = script(1.5)
intro.lines = ["great", "boy"]
在文件的开头。但是,由于上述语法将导入定义,就像它们在文件中声明一样,因此您可以使用:
from time import *
但是还有另一种可能性。sleep()
必须是您声明的函数。如果是这种情况,您必须定义它:
import time
...
time.sleep(self.time_delay)
注意:
- 正如@icktoofay所评论的,您没有使用
方法,因此可能需要添加如下调用:run\u script()
class script(object): # ... def sleep(delay): # implementation
- 您可能对“运行”类感到困惑,因为您不“运行”类,但可以“运行”类的方法。也许你只是说
intro.run_script()
intro.run_script()