<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 31, 2015 at 3:33 PM, Alexander Belopolsky <span dir="ltr"><<a href="mailto:alexander.belopolsky@gmail.com" target="_blank">alexander.belopolsky@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><div class="gmail_quote">On Mon, Aug 31, 2015 at 3:20 PM, Tim Peters <span dir="ltr"><<a href="mailto:tim.peters@gmail.com" target="_blank">tim.peters@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="overflow:hidden">    def __hash__(self):<br>
        if self._hashcode == -1:<br>
<span>            tzoff = self.utcoffset()<br>
            if tzoff is None:<br>
</span>                self._hashcode = hash(self.replace(first=True)._getstate()[0])<br>
            else:<br>
                days = _ymd2ord(self.year, self.month, self.day)<br>
                seconds = self.hour * 3600 + self.minute * 60 + self.second<br>
<span>                self._hashcode = hash(timedelta(days, seconds,<br>
self.microsecond) - tzoff)<br>
</span>        return self._hashcode<br>
<br>
So it's the case that two datetimes that compare true may have<br>
different hashes, when they represent the earlier and later times in a<br>
fold.  I didn't say "it's a puzzle" lightly ;-)</div></blockquote></div><br></span>Yes, it looks like I have a bug there, but isn't fixing it just a matter of moving self.replace(first=True) up two lines?  Is there a bigger puzzle?  Certainly x == y ⇒ hash(x) == hash(y) is the implication that I intend to preserve in all cases.</blockquote></div><br>I think I admitted defeat too soon.  Can you present a specific case where "two datetimes that compare true have different hashes"?  There may be some subtlety due to the fact that we ignore tzinfo in == if it is the same for both sides, but when we compute hash(), we don't know what's on the other side.  It is hard to tell without a specific example.  I thought I got it right when I wrote the code above, but it is possible I missed some case. </div></div>