简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM