Python 代码不显示日期时间,也不抛出任何错误

Python code doesn't show date-time and also doesn't throw any errors

我一直在通过在线课程学习 python,在 oop 部分,我创建了一个简单的银行帐户 class,使用简单的方法,如存款和取款,讲师还展示了如何使用来自 pytz 和 datetime 的日期时间函数。我在 class 中的静态方法不会抛出错误,除非它给了我这个 而不是类似这个 2021-07-30 21:40:47.669274+00:00.astimezone 抛出此属性错误 AttributeError : 'function' 对象没有属性 'astimezone' 同时我也下载了讲师代码,并没有发现我们的代码和讲师代码有任何重大差异运行没有任何问题。

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

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

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