gpt4 book ai didi

python - 无法调用计划作业中的方法

转载 作者:行者123 更新时间:2023-12-01 02:59:58 25 4
gpt4 key购买 nike

我有一个如下所示的类:

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),而不是方法 xyz.deposit 和参数 2300。试试这个:

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

关于python - 无法调用计划作业中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43922701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com