[英]How to call methods using schedule module in python?
我們可以使用這樣的時間表調用一個簡單的 function
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()
但是當我創建一個 class 並調用一個方法時
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
將alarm.wake_up()
替換為alarm.wake_up
。
添加括號時,實際上是調用並執行了方法wake_up
。 但是,當您只執行alarm.wake_up
時,它會創建對該方法的引用,而這正是 schedule 模塊想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.