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
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”.
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.
.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
BetweenMINYEAR
andMAXYEAR
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With