joelhoro
joelhoro

Reputation: 920

Erratic behaviour of datetime.date in python

I got this completely absurd session whilst debugging what appeared to be an erratic behaviour coming from datetime.date

This is the transcript (with some #comments added). Unfortunately I did not manage to find a repro for how I got to my 'd' value (it's obtained through numerous aggregations of randomly generated dates / numbers)

>>> d
[datetime.date(2027, 1, 1), datetime.date(2013, 3, 26)]
>>> d2 = [datetime.date(2027, 1, 1), datetime.date(2013, 3, 26)]
>>> d == d2                      # ok so no misunderstanding
True
>>> min(d)
datetime.date(2027, 1, 1)        # ???
>>> min(d2)
datetime.date(2013, 3, 26)       # fine
>>> max(d)
datetime.date(2013, 3, 26)       # ?!?
>>> max(d2)
datetime.date(2027, 1, 1)        # fine

I know I'm asking a lot but can anyone shed a light on the possible sources for such an absurd situation? I tried restarting my editor (Eric) and it might be related to the debugger, but the issue I am usually getting (i.e. some weird numbers) also happens when I run without the debugger.

Upvotes: 0

Views: 74

Answers (1)

joelhoro
joelhoro

Reputation: 920

Hm... silly me. The issue was that I subclassed datetime.date and so the objects in d were in fact that subclass. Now I need to figure out why the comparison still yields equality.

The takeaway here is that I figured that short of getting a repro, I could use 'pickle' and export my variables for other people to inspect. As I did that I found out that my subclass was being used.

This is what I did:

>>> import pickle
>>> pickle dumps([d,d2])
>>> # some stuff showing the library/subclass

Sorry for the hassle - hope you won't vote me down for that!

Upvotes: 1

Related Questions