Home > Enterprise >  How to fix Python TypeError: 'datetime.datetime' object is not callable?
How to fix Python TypeError: 'datetime.datetime' object is not callable?

Time:07-03

Trying to assign the datetime.datetime.now() value to the self.startDate variable, but getting the error:

TypeError: 'datetime.datetime' object is not callable

!/usr/bin/python3

import datetime
import os
 
class TradingSystem:
    def __init__(self):
        self.startDate = datetime.datetime.now()

ts = TradingSystem()
print("Started trading system, date: {}".format(ts.startDate()))

CodePudding user response:

Try with:

self.startDate = datetime.datetime.now

The problem is that you are already calling the function within your definition and then you're calling it again.

If what you want is to set the start date at the time of instantiation, let the first part as it was (as you posted it) and try:

print("Started trading system, date: {}".format(ts.startDate))

The first option will always print the current date and time, the former will print the date and time of instantiation.

  • Related