簡體   English   中英

如何使用 python 中的調度模塊調用方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM