【问题标题】:Python code doesn't show date-time and also doesn't throw any errorsPython 代码不显示日期时间,也不抛出任何错误
【发布时间】:2021-10-06 09:22:12
【问题描述】:

我一直在在线课程中学习 python,在 oop 部分,我制作了一个简单的银行账户类,其中包含存款和取款等简单方法,讲师还展示了 pytz 和 datetime 中 datetime 函数的使用。我在课堂上的静态方法不会抛出错误,只是它给了我这个 而不是这样的 2021-07-30 21:40:47.669274+00:00.astimezone 抛出这个属性错误 AttributeError: 'function' object has no属性“时区” 同时我还下载了导师代码,没有发现我们的代码有什么大的不同,导师代码运行没有任何问题。

[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))

标签: python datetime oop pytz


【解决方案1】:

我已在下面更正了您的代码。关键点是:

  1. 对于问题: 您指的是函数定义,但实际上并未调用该函数。所以,你应该调用 date_times() 而不是 date_times。

  2. 对于问题 AttributeError: 'function' object has no attribute 'astimezone',问题是相关的。因为你没有调用函数,所以你不能调用函数生成的对象的底层方法。

除此之外,我还删除了一些似乎放错位置的代码。 (self.trans_list = [] --> self.trans_list = []) 和 (account.transc_period()][1] --> account.transc_period())

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().astimezone()))


if __name__ == '__main__':
    account = Account("default", 0)
    account.deposit(1000)
    account.transc_period()
    account.withdraw(500)
    account.transc_period()

【讨论】:

  • 我要添加的唯一评论是交易日期应在交易发生时设置,而不是在打印交易时设置。在此示例中,日期值将是交易实际完成时的打印日期。
猜你喜欢
  • 2016-11-21
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 2017-02-16
  • 2018-09-07
  • 1970-01-01
相关资源
最近更新 更多