OO-programming question

Martijn Faassen m.faassen at vet.uu.nl
Tue Sep 5 16:02:56 CEST 2000


Grant Griffin <g2 at seebelow.org> wrote:
[snip]
> Also, Python's approach allows you to implicitly
> declare the "type".  (Then again, isn't explicit supposed to be better
> than implicit? <wink>.)

A while ago I had the somewhat novel (i.e. Guido called it nuts) idea
of explicitizing the interface. Now, introducing explicit interfaces is
nothing new to Python, but explicit interfaces have an overhead; you
need to write them down:

class Foo:
    __interfaces__ = ['Sequence']

    def __getitem__(self, i):
        ...

    ...

where there's also something like this:

interface Sequence:
    def __getitem__(self, i):
        pass

    ...

Obviously declaring the interface is overhead, and we like Python because
the overhead in code is so small. That's why we like dynamic typing, which
doesn't need any of this.

However, sometimes we'd like the dynamic types to be a bit more explicit. 
My nutty approach was a compromise solution. Classes still declare they
belong to an interface (by name), but the interfaces are not declared
(unless you want them to be):

class Foo:
    __interfaces__ = ['Inquisition']

    def torture(self):
        ...

    def deny(self):
        ...

    def flap_wings(self):
        ...

class Bar:
    __interfaces__ = ['Inquisition']

    def torture(self):
        ...

    def deny(self):
        ...

    def save(self):
        ...

This would implicitly explicitly declare an interface that has the methods
'torture' and 'deny' (shared by all classes that implement this interface).
If at some point you want them to be declared, you can do that still.
But in the mean time you already have them, and you can do things with
them, like some forms of type checking, for instance.

This idea has the advantage that you have some explicitness in interfaces,
with minimal overhead to the programmer. I thought that this was cool,
but nobody else seems to agree. :)

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list