我們可以像這樣使用 schedule 呼叫一個簡單的函式
import schedule
def wake_up():
print("Wake Up! It's 8:00")
schedule.every().day.at("08:00").do(wake_up)
while True:
schedule.run_pending()
但是當我創建一個類并呼叫一個方法時
import schedule
class Alarm():
def wake_up(self):
print("Wake Up!")
alarm=Alarm()
schedule.every().day.at("08:00").do(alarm.wake_up())
while True:
schedule.run_pending()
我明白了
TypeError: the first argument must be callable
uj5u.com熱心網友回復:
替換alarm.wake_up()為alarm.wake_up。
當您添加括號時,您實際上是在呼叫并執行方法wake_up。但是,當您只執行 時alarm.wake_up,它會創建對該方法的參考,這正是 schedule 模塊想要的。
更改在第 7 行:
import schedule
class Alarm():
def wake_up(self):
print("Wake Up!")
alarm=Alarm()
schedule.every().day.at("08:00").do(alarm.wake_up)
while True:
schedule.run_pending()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/391715.html
