简体   繁体   中英

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 (ie some weird numbers) also happens when I run without the debugger.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM