[Python-Dev] datetime module enhancements

Robert Brewer fumanchu at amor.org
Mon Mar 12 01:07:53 CET 2007


Jon Ribbens wrote:
> > Robert Brewer <fumanchu at amor.org> wrote:
> > > One solution that just occurred to me -- and that
> > > skirts the issue of choosing an interpretation --
> > > is that, when comparing date and datetime objects,
> > > the datetime's .date() method is called and the
> > > result of that call is compared to the original
> > > date.
> > +1
> > 
> > ...and it's a decision that can be made independently
> > of how to add or subtract dates.
> 
> I don't like it. It sounds suspiciously like "when
> comparing integers and floats, discard the non-integer
> portion of the float and then compare as integers".

...which can happen quite often, depending on your domain's requirements for significant digits. Integers and floats are numbers, not units, so a more proper analogy would have been, "it sounds like when comparing feet and inches, divide inches by 12 and discard any remainder." Which, again, can happen quite often.

But even feet and inches aren't a good analogy, because people don't say, "six-foot-three is six feet". But they *do* say, "is it Thursday right now?" quite often, and expect a yes-or-no answer, not "cannot compute". A slightly better analogy would be armies and platoons: when you compare armies (on almost any scale except size), you can't say an army is more or less than a platoon in that same army. The platoon is (part of) the army and the army is (made of) the platoons. So order is important only at a given level of granularity:

  Army1 = [P1, P2, P3]
  Army2 = [Px, Py, Pz]
  sorted([Army1, Army2, P1, P2, P3, Px, Py, Pz])
  [Army1, P1, P2, P3,     Px, Py, Army2, Pz]

...that is, it doesn't matter where Army2 appears, as long as it's in the second half of the list. Note that the platoons can still be ordered relative to other platoons in the same army.

Likewise, when comparing dates to date-times (but not when adding them), the mental model of dates and times acts more like nested sets than continuous functions:

  Date1 = Datetimes(1, 2, 3)
  Date2 = Datetimes(x, y, z)
  sorted([Date1, Date2, ...])
  [Date1, T1, T2, T3,     Tx, Ty, Date2, Tz]

...and the above can be achieved by:

    class nested_datetime(datetime):
        def __cmp__(self, other):
            if isinstance(other, datetime):
                return datetime.__cmp__(self, other)
            elif isinstance(other, date):
                return cmp(self.date(), other)
            raise TypeError


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070311/27832859/attachment-0001.html 


More information about the Python-Dev mailing list