[Types-sig] RE: PRE-PROPOSAL: Python Interfaces

Jim Fulton jim@Digicool.com
Tue, 01 Dec 1998 21:03:39 +0000


Tim Peters wrote:
> 

(snip)

> OTOH, unlike e.g. JimF I have nothing against stuffing some default
> implementation into interfaces.  To the contrary, if I *wanted* to say that
> all List implementers implement Stack too, I think
> 
>    class List(Interface, Stack):  #??? spelling of interface

I assume that List, Stack and Interface are all interfaces.
BTW, I ended up spelling the base interface 'Interface.Base'.

>       __implements__ = Stack      #??? ... hierarchy is unclear

What does this want to mean?

>       def pop(self): raiseDeferredError()
>       def append(self, thing): raiseDeferredError()
>         ...
>       def push(self, thing): self.append(thing)
> 
> is the obvious way to do it.  This doesn't preclude a different
> implementation of push, it simply reflects the truth that List *can* give
> you an implementation of push guaranteed to meet push's spec, assuming only
> that the implementations of List.append and List.pop meet *their* specs.
> 
> For a simpler example, after rich comparisons are in
> 
>     class SaneOrdering(Interface):
>         def __eq__(self, other): raiseDeferredError()
>         def __ne__(self, other): return not self.__eq__(other)
> 
> would save 99% of the SaneOrderings ever created the pointless and
> error-prone bother of implementing __ne__ themselves.  That is, if the
> post-condition for SaneOrdering.__ne__(self, other) is
> 
>     assert Result == not self.__eq__(other)
> 
> what better way to encourage correct software than to provide a default
> implementation that guarantees it without further ado?  Many methods in rich
> interfaces exist simply for user convenience, having obvious (but clumsy to
> type out) implementations in terms of other methods.

I've tried to provide a compromise in the implementation I released 
earlier today.

I provide a method, 'defered' on interfaces that returns a defered classes.
For example:

  class StackInterface(Interface.Base):
     
    def pop(self):
       "Remove and return"

    def append(self, v):
       "Add an object to the stack"

    def push(self, v):
       "Add an object to the stack"
 
  class Stack(StackInterface.defered()):

    def push(self, v): ...
    def pop(self):  ...

  s=Stack()
  s.append(1) 

raises a BrokenImplementation error rather than
an AttributeError because the author of the Stack 
class failed to provide an append method.

I've also suggested that if a method definition in
an interface contains a body (other than a doc string), 
then perhaps a non-defered method will be provided when 
a defered class is genrated from the interface.  Note 
that the body should only use attributes defined by the 
interface.  For example, if the append method 
defined in StackInterface above was:

    def append(self, v):
       "Add an object to the stack"
       self.push(v)

then the defered class would have a non-defered implementation
of append and:

  a.append(1)

would suceed.

Jim

--
Jim Fulton           mailto:jim@digicool.com
Technical Director   (540) 371-6909              Python Powered!
Digital Creations    http://www.digicool.com     http://www.python.org

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.