比较Python中的两个日期对象:TypeError:’datetime.date’和’
这不应该太难,但我似乎无法让它工作.
我想在 Python中比较两个datetime.date类型,但我不断收到类型错误: from datetime import date class Vacancy(object): def __init__(self,date): #date is a datetime string of format 2017-13-03T00.00.000Z self.date = datetime.strptime(date[:-1],'%Y-%m-%dT%H:%M:%S.%f').date() def getDate(self): return self.date all_objects = [o1,o2,o3,o4,...] #contains objects of type Vacancy for o in all_objects: earliestDate = date(2020,1,1) if o.getDate() < earliestDate: earliestDate = o.getDate() print(earliestDate) TypeError:’<' 'datetime.date'和'method'的实例之间不支持 这对我没有意义,因为: 我能做错什么? 编辑:为all_objects中的对象添加了类示例代码 EDIT2:正如你们许多人所指出的那样,确实缺少'()’.在我的实际代码中,我通过执行earliestDate = o.getDate来分配值的方法instad.下次我会尝试对我的代码更加真实.感谢大家提供的见解,因为我确实来自Java,但我还没有完全理解Python. 解决方法
TypeError应该为您提供解决此问题所需的所有信息.以下是解释它的方法:
TypeError: '<' not supported between instances of 'datetime.date' and 'method' >’<'不支持意味着您在使用<时遇到错误运算符,你已经知道了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |