2013-04-08 5 views
3

막혔습니다. 그 날은 어딘가에서 int로 덮어 쓰여지고있는 것처럼 보입니다. 그러나 어디에서? 하루가 int가되는 곳은 어디입니까?date.day()가 TypeError를 반환합니다. '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 
+0

제발 추적 *을 포함하십시오. 오류가없는 곳에서 오류가 어디에 있는지를 추측하기는 어렵습니다. –

답변

7

.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'> 

datetime.date documentation에서 : 그것은 (A property 같은) 데이터 기술자로 구현됩니다

Instance attributes (read-only):

date.year
Between MINYEAR and MAXYEAR inclusive.

date.month
Between 1 and 12 inclusive.

date.day
Between 1 and the number of days in the given month of the given year.

따라서, 읽기 전용으로 만들기 위해 당신이 본 오류 TypeError: 'getset_descriptor' object is not callable.

관련 문제