datetime module enhancements

On 3/12/07, Christian Heimes <lists at cheimes.de> wrote:
datetime before date: < datetime during date: in datetime after date: >
datetime <= date and datetime => date should raise a TypeError. The result is ambiguous. A date with time is never equal to a date.
Jim Jewett wrote:
As a practical matter, sorting uses <=
Are you sure? >>> class C(object): ... def __init__(self, x): ... self.x = x ... def __lt__(self, other): ... return self.x < other.x ... def __le__(self, other): ... raise TypeError() ... def __repr__(self): ... return 'C(%r)' % self.x ... >>> sorted([C(3), C(2), C(1)]) [C(1), C(2), C(3)] Looks like it's just using < to me. (And thanks to Collin and Christian for jumping on the patch already.) STeVe -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy

Steven Bethard schrieb:
datetime <= date and datetime => date should raise a TypeError. The result is ambiguous. A date with time is never equal to a date.
Jim Jewett wrote:
As a practical matter, sorting uses <=
Are you sure?
It depends what tp slot is defined. The C api supports either comparison (cmp style -1, 0, 1) or rich comparison (func(self, other, operator)). Christian

On 3/12/07, Christian Heimes <lists@cheimes.de> wrote:
Steven Bethard schrieb:
datetime <= date and datetime => date should raise a TypeError. The result is ambiguous. A date with time is never equal to a date.
Jim Jewett wrote:
As a practical matter, sorting uses <=
Are you sure?
It depends what tp slot is defined. The C api supports either comparison (cmp style -1, 0, 1) or rich comparison (func(self, other, operator)).
Fair enough. My only point was that as long as __lt__ is defined, __le__ can throw a TypeError() and it won't break sorted(). STeVe -- I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a tiny blip on the distant coast of sanity. --- Bucky Katt, Get Fuzzy

On 3/12/07, Steven Bethard <steven.bethard@gmail.com> wrote:
On 3/12/07, Christian Heimes <lists@cheimes.de> wrote:
Steven Bethard schrieb:
datetime <= date and datetime => date should raise a TypeError. The result is ambiguous. A date with time is never equal to a date.
But it can still be <=, by being <. I would personally be OK with just saying that (year, month, day) sorts less than (year, month, day, ...) regardless of time, simply because of the type -- but I admit that would be arbitrary. ...
Fair enough. My only point was that as long as __lt__ is defined, __le__ can throw a TypeError() and it won't break sorted().
Mea culpa. I was mis-remembering, and thought that even this would break because of sort stability. -jJ
participants (3)
-
Christian Heimes
-
Jim Jewett
-
Steven Bethard