Jar*_*obs 5 python datetime python-3.x
我想比较两个日期.此代码有效:
import datetime
todays_date = datetime.date.today()
date1 = datetime.date(2006, 3, 15)
date2 = datetime.date(2009, 4, 30)
print(date1 != date2)
print(date1 == 0)
Run Code Online (Sandbox Code Playgroud)
它产生:
True
False
Run Code Online (Sandbox Code Playgroud)
那些代码不起作用,我不知道为什么:
import datetime
todays_date = datetime.date.today()
date1 = datetime.date(2006, 3, 15)
date2 = datetime.date(2009, 4, 30)
print(date1 != date2)
print(date1 >= 0)
Run Code Online (Sandbox Code Playgroud)
它会产生以下错误:
File 'datetime.py', Line 363: AttributeError: 'int' object has no attribute '__name__'
Run Code Online (Sandbox Code Playgroud)
请注意,所有我改变的是==对>=的,为什么不相等的比较结果True或False,而不是在错误comparaison结果还大吗?
我将不胜感激任何帮助!
abc*_*ccd 10
这是因为比较方法的定义方式.
这里的副本源代码的time对象:
def __eq__(self, other):
if isinstance(other, time):
return self._cmp(other, allow_mixed=True) == 0
else:
return False
def __ge__(self, other):
if isinstance(other, time):
return self._cmp(other) >= 0
else:
_cmperror(self, other)
Run Code Online (Sandbox Code Playgroud)
__eq__False当它不是另一个time实例时返回,而__ge__调用_cmperror则定义如下:
def _cmperror(x, y):
raise TypeError("can't compare '%s' to '%s'" % (
type(x).__name__, type(y).__name__))
Run Code Online (Sandbox Code Playgroud)
虽然这个答案已经得到了一些积极的分数,但我错过了阅读你的问题,你使用的是date对象,而不是time对象.
首先,date对象,不同time的对象,他们实现了同为__eq__和__ge__.它们都实际返回NotImplemented,因此date对象的方法没有什么特别之处:
def __eq__(self, other):
if isinstance(other, date):
return self._cmp(other) == 0
return NotImplemented
def __ge__(self, other):
if isinstance(other, date):
return self._cmp(other) >= 0
return NotImplemented
Run Code Online (Sandbox Code Playgroud)
然而,不同之处在于与其他方法int的__eq__比较.int返回False当一个对象具有无可比类型__eq__和NotImplemented对__ge__.
在NotImplemented通过返回date将导致在回退int的方法.由于int始终是相等的,date == 0不会导致错误.
这是一个例子:
class LikeDate:
def __eq__(self, other):
if isinstance(other, LikeDate):
return True
else:
return NotImplemented
def __ge__(self, other):
if isinstance(other, LikeDate):
return True
else:
return NotImplemented
class LikeInt:
def __eq__(self, other):
if isinstance(other, LikeInt):
return True
else:
return False
def __ge__(self, other):
if isinstance(other, LikeInt):
return True
else:
return NotImplemented
a = LikeDate()
b = LikeInt()
print(a == b) # False
print(a == 0) # False, because int provides an __eq__ method that returns False
print(a >= 0) # Error, because nether LikeDate nor int provides a definite comparison for __ge__
print(a >= b) # Error, because neither objects provide a comparable __ge__
Run Code Online (Sandbox Code Playgroud)
如果你不知道是什么return NotImplemented,这里有一个简短的解释和文档的引用:
当二进制[(
__eq__,__ge__...)](或就地)方法返回时NotImplemented,解释器将尝试对另一种类型(或其他一些后备,取决于运算符)的反射操作.如果所有尝试都返回NotImplemented,则解释器将引发适当的异常.错误地返回NotImplemented将导致误导性错误消息或NotImplemented返回到Python代码的值.
当NotImplemented从二进制方法返回,它指的是二进制的方法是不能够自己与目标的类型进行比较.二进制方法的结果将取决于其他对象的二进制方法.如果两个对象都返回NotImplemented,则引发错误.
您正在尝试date1与 0 进行比较。
这是行不通的,因为date1是 a datetime.date(看看你最初是如何定义它的)。所以基本上你是在比较 adate和 an int。
您只能比较相似的类型。
| 归档时间: |
|
| 查看次数: |
302 次 |
| 最近记录: |