1. 程式人生 > >Python錯誤解決 TypeError: first arg must be callable

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 """
schedule.every
().day.at("10:30").do(job)

方法名後面不帶括號,就可以執行成功。
網上查找了相關的解釋,說是帶括號代表方法的返回值,不帶括號代表方法本身。
可能想問,要是定時任務方法帶著引數怎麼辦?
再回過來看,第二個引數*arg 是可以為方法新增引數的。