Python 类型错误:“datetime.datetime”对象不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25275094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:59:52  来源:igfitidea点击:

TypeError: 'datetime.datetime' object is not callable

pythontime

提问by gdogg371

I have some Python code that iterates through all the days between two start dates. The start date is always November 1st and the end date is always May 31st. However, the code iterates through years. My code is as so:

我有一些 Python 代码可以遍历两个开始日期之间的所有日子。开始日期始终为 11 月 1 日,结束日期始终为 5 月 31 日。但是,代码会迭代多年。我的代码是这样的:

import time
from datetime import datetime
from datetime import date, timedelta as td

list1 = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013]
list2 = [2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014]


for x, y in zip(list1, list2):
    print "list1 - " + str(x)
    print "list2 - " + str(y)


    d1 = date(x,11,01)
    d2 = date(y,5,31)

    delta = d2 - d1



    for i in range(delta.days + 1):

        time1 =  str(d1 + td(days=i))
        time2 = time1.split("-", 1)[0]
        time3 = time1.split("-", -1)[1]
        time4 = time1.rsplit("-", 1)[-1]

        time2 = int(time2)
        time3 = int(time3)
        time4 = int(time4)

        date = datetime(year=time2, month=time3, day=time4)

...some processing here...

This code works fine until the first cycle is completed. It then prints the next two values of 'list1' and 'list2' as 2001 and 2002 to the log, before producing the following error:

这段代码可以正常工作,直到第一个周期完成。然后,在产生以下错误之前,将“list1”和“list2”的下两个值作为 2001 和 2002 打印到日志中:

Traceback (most recent call last):
  File "C:\Python27\newtets\newtets\spiders\test3.py", line 17, in <module>
    d1 = date(x,11,01)
TypeError: 'datetime.datetime' object is not callable

It doesn't seem to be resolving the year assigned to the variable 'x' on this second pass through. Can anyone tell me why this is?

在第二次通过时,它似乎没有解析分配给变量“x”的年份。谁能告诉我这是为什么?

Thanks

谢谢

采纳答案by alecxe

This is because you are having a variable called datethat is shadowing imported datetime.date. Use a different variable name.

这是因为您有一个名为dateshadowing import的变量datetime.date。使用不同的变量名称。

Demo:

演示:

>>> from datetime import date, datetime
>>> date(01,11,01)
datetime.date(1, 11, 1)
>>> date = datetime(year=2014, month=1, day=2)
>>> date(01,11,01)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'datetime.datetime' object is not callable

回答by blitzen

I think that the following line:

我认为以下行:

date = datetime(year=time2, month=time3, day=time4)

is the issue. Here, you are re-defining dateto have a different value (that can't be called) to the date class (which could be).

是问题。在这里,您正在重新定义日期以具有与日期类(可能是)不同的值(无法调用)。

On the 'second pass through', it gets to:

在“第二次通过”时,它会:

d1 = date(x,11,01)

and dateisn't what it used to be (it can't be called), and so you get the error.

并且date不是它以前的样子(它不能被调用),所以你会得到错误。

Maybe change variable name to be something else, e.g. dte?

也许将变量名称更改为其他名称,例如 dte?