[BangPypers] Should equality (__eq__) be dependent on the typeof object?

Noufal Ibrahim noufal at nibrahim.net.in
Tue Sep 10 07:47:30 CEST 2013


Shabda Raaj <shabda at agiliq.com> writes:

> Eg: See this:
>
> http://nbviewer.ipython.org/5151306
>
> I was expecting datetime.date.today() == datetime.datetime.today()
>
> to give me a True. (It is false).

I'd expect it to be False. There will be a small amount of time between
the two invocations and the time will change

>>> x, y = (datetime.datetime.now(),datetime.datetime.now())
>>> x
datetime.datetime(2013, 9, 10, 11, 14, 16, 662050)
>>> y
datetime.datetime(2013, 9, 10, 11, 14, 16, 662102)
>>> 

> For example this works as I expect:
>
> http://nbviewer.ipython.org/5151398
>
> However looks like __eq__ is doing a isinstance check - I would expect
> things which are "duck-type" equal to pass the __eq__ check.
>
> http://hg.python.org/cpython/file/5fb700ca3fd5/Lib/datetime.py

It's doing the isinstance to error out if you try to compare a datetime
against something like an integer or something. If that doesn't happen,
it goes into _cmp and does the actual work.

-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list