[Types-sig] Protocols

Michel Pelletier michel@digicool.com
Wed, 14 Mar 2001 10:11:32 -0800 (PST)


(Warning, sloppy UML linked)

On Wed, 14 Mar 2001, Paul Prescod wrote:

> Michel Pelletier wrote:
> >
> > This is what I think about what thinking about bringing types to python:
> > to me, protocol means the same thing as interface.  Is this off-base?
>
> No, I think that most people use the words interchangably.

Good.

> > > The minimalist spirit is to lean towards "too lightweight." Therefore I
> > > propose that the protocol for mappings require only __getattr__,
> > > .keys(), .values() and .items(). The protocol for sequences requires
> > > only __getattr__.
> >
> > Really?  Defining a sequence as getattr seems to broad to me.  To me,
> > something that only implements getattr is an object.  Of course, this
> > doesn't work for types, which are objects.  I would think a sequence was
> > defined by __len__ and __getitem__.
>
> Doh! I meant __getitem__. I'm not sure about __len__ . There are such
> things as infinite sequences.

Yes, that makes sense.

> > We've made a couple of guesses along these lines, should I post them
for
> > discussion?
>
> Yes please!

Okay, here are some experimental concepts we've worked with.  Keep in mind
all of you, that these are just the way things look like to *us*.  There
may be better names for these interfaces.  We may have factored them
different than you would. etc. etc.  This is the kind of thing the BDFL
would pronounce on if there were many different opinions.  For this email,
I will use my notional "interface" syntax, but our prototypes use the Zope
reference implementation:

    interface Mutable:
        """Mutable objects
        """
    interface Comparable:
        """Objects that can be tested for equality
        """
    interface Orderable(Comparable):
        """Objects that can be compared with < > == <= >= !="""

    interface Hashable:
        """Objects that support hash"""

    interface HashKey(Comparable, Hashable):
        """Objects that are immutable with respect to state that
        influences comparisons or hash values"""

    interface Mapping:
        "anything supporting __getitem__"

    interface Sized:
        "anything supporting __len"

    interface MutableMapping(Mutable):
        "Has __setitem__ and __delitem__"

    interface Sequence(Mapping):
        "Keys must be integers in a sequence starting at 0."

    interface Sequential(Sequence):
        "Keys must be used in order"


I have captured this in a UML model to make the concept more clear in
relation to three simple types: tuple, list, and dictionary (for bonus
points, you can try and guess which of these interfaces these three types
implement before you look at the model).

http://www.zope.org/Members/michel/PythonUML.png

-Michel