Date manipulation and Java 'interface' equivalents

Alex Martelli aleaxit at yahoo.com
Sun Nov 5 18:18:02 EST 2000


"Martin Christensen" <knightsofspamalot-factotum at mail1.stofanet.dk> wrote in
message news:87puka8qaw.fsf at fangorn.stofanet.dk...
> Howdy,
>
> There are two matters for which I would like clarification.
>
> First of all, I am doing some statistics with a PostgreSQL database,
> and for this I need to manipulate some dates, ie. add a week to this
> date or subtract a month from another. I didn't find any obvious
> candidates for this kind of date math in the library reference. Now,
> _of course_ this can be done in Python rather than forcing me to use
> the DBMS for this, but... erm... how?

I suggest you take a look at M-A Lemburg's excellent mxDateTime
package, at
http://starship.python.net/~lemburg/mxDateTime.html

I think you'll see it does all you desire, easily & speedily.  It
will also interoperate well with mxODBC (which you'll find at
http://starship.python.net/~lemburg/mxODBC.html) for work
with many DB's, though I'm not sure PostgreSQL is among them.


> Second question: is there a Python equivalent of Java interfaces? In a

Python does not currently formalize the idea of "interface", just
as Java does not formalize its crucial part (the "programming by
contract" expression of pre-, post-, and invariant-conditions of
the interface's various methods).  But, in each case, the concept is
easily realized and plays an important role.

For example, consider the idea of "a sequence of items that
can be iterated on, one after the other".  In Java, you can
formally express the syntax in which this must be realized,
but, regarding the semantics (what preconditions, post-
conditions, and invariant each method applies), you're left
to rely on respecting certain conventions -- lacking parametric
genericity ("templates"), you can't even really express the
static-typing aspects of the iteration, despite the fact that
the Java model makes such a big thing of them.

In Python, the syntax itself is also only by-convention -- "a
sequence is an object which implements a __getitem__
method, to be called with a single (int) argument that goes
from 0 up; it will return the next item, except that, if the
sequence is finished, it will raise IndexError".  Python's
dynamic typing means the typing issues are moot, of course.

The point is that your object is a sequence if, and only
if, it implements a method of this description; it makes
no difference whether it does so directly, or by inheriting
by whatever other class you choose.  This can be called
"signature-based polymorphism".  Like all typing issues
in Python, it's only checked dynamically (just as, in Java,
are such things as whether the Object references you're
returning can correctly be cast to whatever it is that you
need; only dynamic checks are done) -- in exchange for
which, you get extreme flexibility and fluidity compared
to a mostly-statically-typed language.


> matter of months I'll be constructing a web based sales and customer
> relations thingy, and a similar feature would really make things
> easier. I haven't decided on the implementation language yet, but
> this is one of the things that make Java more appealing.

The formalization of the 'interface' concept in Java does
not really buy you all that much in practice compared
with Python's pragmatically-equivalent (more dynamic
and fluid) approach.  Some, yes, but not much.  If you
ever try to call a non-existent method on an object,
you'll get a runtime exception in Python -- just as you
would in Java if the method existed but you violated
some semantic precondition of its call, or a downcast
from Object failed.  Java gives a mix of static and dynamic
checks; Python does it all dynamically -- not quite as
efficient, but with a lot of ease & fluidity in exchange.

Despite my background being in statically typed languages,
I'm coming to be convinced that the tradeoff is an overall
win for dynamic typing in most real situations...


Alex






More information about the Python-list mailing list