简体   繁体   English

python中datetime.date的异常行为

[英]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 在调试似乎来自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”值的再现形式(它是通过随机生成的日期/数字的大量汇总获得的)

>>> 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. 我尝试重新启动编辑器(Eric),它可能与调试器有关,但是当我在没有调试器的情况下运行时,通常会遇到的问题(即一些奇怪的数字)也会发生。

Hm... silly me. 嗯...愚蠢的我。 The issue was that I subclassed datetime.date and so the objects in d were in fact that subclass. 问题是我将datetime.date子类化,因此d中的对象实际上是该子类。 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! 对不起,很麻烦-希望您不要为此而拒绝我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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