Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date.day() returns TypeError: 'int' object is not callable

I'm stuck. It appears that day is being overwritten as an int somewhere. But where? Where is day becoming an 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
like image 945
Ryan Linton Avatar asked Apr 08 '13 18:04

Ryan Linton


People also ask

How do I fix TypeError int object is not callable?

How to resolve typeerror: 'int' object is not callable. To resolve this error, you need to change the name of the variable whose name is similar to the in-built function int() used in the code. In the above example, we have just changed the name of variable “int” to “productType”.

Why is my int object not callable?

The “int object is not callable” error occurs when you declare a variable and name it with a built-in function name such as int() , sum() , max() , and others. The error also occurs when you don't specify an arithmetic operator while performing a mathematical operation.


Video Answer


1 Answers

.day is not a method, you do not need to call it. Only .weekday() is a method.

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

This works just fine:

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

From the datetime.date documentation:

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.

It is implemented as a data descriptor (like a property) to make it read-only, hence the TypeError: 'getset_descriptor' object is not callable error you saw.

like image 123
Martijn Pieters Avatar answered Oct 08 '22 18:10

Martijn Pieters