[Types-sig] Interface PEP

Michel Pelletier michel@digicool.com
Wed, 14 Mar 2001 09:33:46 -0800 (PST)


On Wed, 14 Mar 2001, Sverker Nilsson wrote:

> I was wondering why, or if, the new concept of "interface" should be
> introduced. There is already the concept of "type" in Python. I
> thought that concept could/would/should be extended to cover more
> general interfaces.
>
> One would say then, I suppose, instead of
>
> interface x...
>
> something like
>
> typedef x...
>
> and the rest of the def would be essentially the same, I think.
> Instead of the __implements__ special attribute, one would use
> an attribute name more alluding to the type concept, I might want
> to call it __type__.
>
> The type() builtin would return, as usual, InstanceType if the
> __type__ special attribute was not defined. Otherwise it would return
> what __type__ returned. - which would be a user defined type (aka
> interface) or even a builtin type, if the class wants to claim it
> emulates a built-in type.
>
> (Claiming is one thing of course, really doing it is another -
> there can never be any guarantee, I suppose.)
>
> So.. what would be the reason to have interface another concept than
> type?
>
> Or should they be the same concept?

They are different concepts.  Read the section in the PEP on "Classes and
Interfaces".  One class, or "type" of object, can implement multiple
interfaces, and a "type" consisting of an many mixed in classes can be
used to implement just one interface.

Many different objects of different types many also implement the *same*
interface.  For example, lots of different types of objects can be a
sequence or a mapping.  "sequence" and "mapping" are not types like lists
and dictionaries, they are just protocol descriptions, or interfaces.  In
Zope, we may have a RelationalUserFolder, and a FileUserFolder, both
completely different implementations and different types, but they both
implement the UserFolderInterface.

In many ways, interfaces achieve a lot of what a stronger typing system in
python strives to achieve.  This doesn't mean they can't co-exist, and I
posit any idea type system in python would be intimately involved with the
interfaces (often refered to as "protocols") of objects.

-Michel