Comparing two date objects in Python: TypeError: '<' not supported between instances of 'datetime.date' and 'method'
The TypeError
should give you all information you need to solve this problem. Here's how to interpret it:
TypeError: '<' not supported between instances of 'datetime.date' and 'method'
- The
'<' not supported
means that you got the error when using the<
operator, as you already know. - The comparison doesn't work because one of the things you are comparing is not a
datetime.date
instance. You already got this, too. - The
method
type is what you get if you would useo.getDate
instead ofo.getDate()
. In Python you can pass around methods as values if you like, just like lambdas or functions. This is not what you want in this case however, so make sure you use()
everywhere you want to call a method, even if it doesn't take any arguments. - The order of the types in the error message is also interesting. That
datetime.date
comes beforemethod
mean that the date was on the left side and the problematic value was on the right side. In your case, theearliestDate
is holding amethod
instead of adatetime.date
. - Now that we know that
earliestDate
is the problem, where is it updated?earliestDate = date(2020, 1, 1)
is clearly a date, but how aboutearliestDate = o.getDate()
? It's using parentheses, soo.getDate()
must be returning amethod
. - Given your code, the
Vacancy
will always haveself.date
set to a date, or an exception will be thrown (something likeValueError: time data 'xxx' does not match format '%Y-%m-%dT%H:%M:%S.%f'
). I'm guessing your code looks different and the initialization forVacancy
is wrong somehow. This is the benefit of providing a MCVE :)
You overwritten the definition of date, try this (maintaining the datetime namespace with an alias: dt). A good practice would be to not name local or member variables with the same name of functions and objects of the libraries you import (you imported date from datetime and then use date
as the argument of the init).
import datetime as dt
class Vacancy(object):
def __init__(self, date):
self.date = date
def getDate(self):
return self.date
all_objects = [o1, o2, o3, o4, ...] #contains objects of type Vacancy
for o in all_objects:
earliestDate = dt.date(2020, 1, 1)
if o.getDate() < earliestDate:
earliestDate = o.getDate()
print(earliestDate)
An extra observation is that in python there is no need of defining getters and setters, since the variables are public. It is better if you just:
import datetime as dt
class Vacancy(object):
def __init__(self, date):
self.date = date
all_objects = [o1, o2, o3, o4, ...] #contains objects of type Vacancy
for o in all_objects:
earliestDate = dt.date(2020, 1, 1)
if o.date < earliestDate:
earliestDate = o.date
And if you want to be sure the date member variable is not modified, you can do something like this:
class Vacancy(object):
def __init__(self, date):
self.date = date
def getMinDate(self, other_date):
if self.date < other_date:
return dt.date(self.date)
else:
return other_date
all_objects = [o1, o2, o3, o4, ...] #contains objects of type Vacancy
earliestDate = dt.date(2020, 1, 1)
for o in all_objects:
earliestDate = o.getMinDate(earliestDate)