date.day()返回TypeError:';int';对象不可调用

date.day()返回TypeError:';int';对象不可调用,date,datetime,python-2.7,typeerror,Date,Datetime,Python 2.7,Typeerror,我卡住了。这一天似乎在某处被改写为int。但是在哪里呢?day在哪里变成int from datetime import * start_date = date(1901, 1, 1) end_date = date(2000, 12, 31) sundays_on_1st = 0 def daterange(start_date, end_date): for n in range(int ((end_date - start_date).days)): yield

我卡住了。这一天似乎在某处被改写为int。但是在哪里呢?day在哪里变成int

from datetime import *

start_date = date(1901, 1, 1)
end_date = date(2000, 12, 31)
sundays_on_1st = 0

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

for single_date in daterange(start_date, end_date):

    # type(single_date) => <type 'datetime.date'>
    # type(date.day()) => TypeError: 'getset_descriptor' object is not callable
    # type(single_date.day()) => TypeError: 'int' object is not callable
    # ಠ_ಠ 

    if single_date.day() == 1 and single_date.weekday() == 6: 
        sundays_on_1st += 1                                     

print sundays_on_1st
从日期时间导入*
开始日期=日期(1901年1月1日)
结束日期=日期(2000年12月31日)
星期日第1天=0
def日期范围(开始日期、结束日期):
对于范围内的n(int((结束日期-开始日期).days)):
产量开始日期+时间增量(n)
对于日期范围中的单个日期(开始日期、结束日期):
#类型(单一日期)=>
#type(date.day())=>TypeError:“getset\u描述符”对象不可调用
#type(single_date.day())=>TypeError:“int”对象不可调用
# ಠ_ಠ 
如果单个日期.day()==1和单个日期.weekday()==6:
星期日在第一日+=1
在1号打印星期日

。day
不是一个方法,您不需要调用它。只有
.weekday()
是一个方法

if single_date.day == 1 and single_date.weekday() == 6: 
    sundays_on_1st += 1                                     
这很好:

>>> for single_date in daterange(start_date, end_date):
...     if single_date.day == 1 and single_date.weekday() == 6:
...         sundays_on_1st += 1
... 
>>> print sundays_on_1st
171
>>> type(single_date.day)
<type 'int'>
>>对于日期范围中的单个日期(开始日期、结束日期):
...     如果single_date.day==1,single_date.weekday()==6:
...         星期日在第一日+=1
... 
>>>在1号打印星期日
171
>>>类型(单日)
从:

实例属性(只读):

日期.年份

介于
MINYEAR
MAXYEAR
之间

date.month

介于1和12之间(含1和12)

date.day

介于1和给定年份中给定月份的天数之间

它被实现为一个数据描述符(如
属性
),以使其成为只读的,因此
类型错误:“getset\u descriptor”对象是不可调用的
错误。请务必包括回溯;如果没有它,很难猜测错误可能在哪里。