【发布时间】:2021-10-06 09:22:12
【问题描述】:
我一直在在线课程中学习 python,在 oop 部分,我制作了一个简单的银行账户类,其中包含存款和取款等简单方法,讲师还展示了 pytz 和 datetime 中 datetime 函数的使用。我在课堂上的静态方法不会抛出错误,只是它给了我这个
[import datetime
import pytz
class Account:
@staticmethod
def _time():
date = datetime.datetime.utcnow()
return pytz.utc.localize(date)
def __init__(self, name, balance):
self.name = name
self.balance = balance
self.trans_list = \[\]
def print_balance(self):
print("Current balance is {}".format(self.balance))
def deposit(self, amount):
if amount > 0:
self.balance += amount
self.print_balance()
self.trans_list.append((Account._time, amount))
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
self.print_balance()
self.trans_list.append((Account._time, -amount))
def transc_period(self):
for date_times, amount in self.trans_list:
if amount > 0:
tran_type = "deposited"
else:
tran_type = "withdrawn"
amount *= -1
print("{:6} {} on {} )".format(amount, tran_type, date_times))
if __name__ == '__main__':
account = Account("default", 0)
account.deposit(1000)
account.transc_period()
account.withdraw(500)
account.transc_period()][1]
astimezone 行出现在 transc_period 的最后一行
[print("{:6} {} on {} )".format(amount, tran_type, date,date.astimezone()))][1]
【问题讨论】:
-
使用参数列表
()调用函数,即使列表为空。Account._time是一个函数,Account._time()调用该函数。 -
你没有调用函数,只是传递了函数本身。你想调用函数
self.trans_list.append((Account._time(), amount))