Python错误解决 TypeError: first arg must be callable
在写定时任务的时候遇到了这样的错误
import schedule
import time
def job():
print("start job")
print("hello")
schedule.every().day.at("13:01").do(job())
while True:
schedule.run_pending()
time.sleep(2)
错误如下:
Traceback (most recent call last): File "***/pyscript/TimeTask.py", line 11, in <module> schedule.every().day.at("13:01").do(job()) File "***/venv/lib/python3.6/site-packages/schedule/__init__.py", line 385, in do self.job_func = functools.partial(job_func, *args, **kwargs) TypeError: the first argument must be callable
查看do这个方法到底长什么样
def do(self, job_func, *args, **kwargs):
""" Specifies the job_func that should be called every time the job runs. Any additional arguments are passed on to job_func when the job runs. :param job_func: The function to be scheduled :return: The invoked job instance """
self代表调用函数的实例,相关链接:http://python.jobbole.com/81921/
其实job_func 才是第一个参数,也就是我们需要执行的方法。
在仔细查看官方实例之后发现他是这样调用的
官方链接:https://schedule.readthedocs.io/en/stable/
schedule.every().day.at("10:30").do(job)
方法名后面不带括号,就可以执行成功。
网上查找了相关的解释,说是带括号代表方法的返回值,不带括号代表方法本身。
可能想问,要是定时任务方法带着参数怎么办?
再回过来看,第二个参数*arg 是可以为方法添加参数的。
还没有评论,来说两句吧...