无法在计划作业中调用方法

Unable to call a method within a schedule job

我有一个 class 看起来像这样:

class Account(object):
    """A simple bank account"""

    def __init__(self, balance=0.0):
        """
        Return an account object with a starting balance of *balance*.
        """
        self.balance = balance

    def withdraw(self, amount):
        """
        Return the balance remaining after withdrawing *amount* dollars.
        """
        self.balance -= amount
        return self.balance

    def deposit(self, amount):
        """
        Return the amount remaining after depositing *amount* dollars.
        """
        self.balance += amount
        return self.balance

我会在xyz中初始化它:

xyz = Account(balance=6000)
xyz.balance
> 6000

我还有哑打印功能:

def thing():
    print("I am doing a thing...")

当我尝试在 schedule 流程中调用 deposit 方法时:

import schedule

# this works
# schedule.every(5).seconds.do(thing)

# this doesn't work
schedule.every(5).seconds.do(xyz.deposit(2300))

while True:
    schedule.run_pending()

我收到以下错误:

TypeError: the first argument must be callable

有什么想法吗?甚至可以在计划流程中调用方法吗?

不熟悉 schedule,但 do() wants 似乎是可调用的,即方法。您给它 xyz.deposit(2300) 的 return 值,而不是方法 xyz.deposit 和参数 2300。试试这个:

schedule.every(5).seconds.do(xyz.deposit, 2300)