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

Solution for Python code doesn’t show date-time and also doesn’t throw any errors
is Given Below:

I’ve been learning python from an online course and in the oop section I made a simple bank account class with simple methods like deposit and withdrawal and the instructor also showed the use of datetime function from pytz and datetime. My static method in class doesn’t throw an error except it gives me this <function “Class-Name”._time at 0x00000251A4D72790> instead of something like this
2021-07-30 21:40:47.669274+00:00 and the .astimezone throws this attribute error AttributeError: ‘function’ object has no attribute ‘astimezone’
Meanwhile I also downloaded the instructors code and couldn’t find any major difference in our code and the instructors code ran without any issues.

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

The astimezone line comes in transc_period last line

 [print("{:6} {} on {}   )".format(amount, tran_type, date,date.astimezone()))][1]

I have corrected your code below. The key points were:

  1. For the issue: <function “Class-Name”._time at 0x00000251A4D72790> you were referring to the function definition, but not actually invoking the function. So, you should call date_times() instead of date_times.

  2. For the issue AttributeError: ‘function’ object has no attribute ‘astimezone’, the issue is related. Because you did not call the function, you could not call the underlying methods to the object the function generates.

Apart from that I removed a few parts of the code that seemed to be misplaced. (self.trans_list = [] –> self.trans_list = []) and (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()