[Types-sig] A type checking system in Python

Paul Prescod paulp@ActiveState.com
Wed, 21 Mar 2001 13:59:00 -0800


Huaiyu Zhu wrote:
> 
> Thanks to Clark and Robin for encouragement.  I was not aware that type-sig
> was active when I wrote those comments and implementation (Aug 2000).  

Sorry, I didn't realise that this was an older post that had been
revived! I thought you were in sync with our current efforts. 

> ...
> This may be more flexible than what is normally called polymorphism.  So
> let's call it strong polymorphism.  In any case, it is routinely used in
> Python.  It is not supported by Java interface.  In Java, I do not believe
> you can define an interface in user code, and then retro-actively declare
> that some imported class satisfy this interface.  I would not want a
> typechecking system in Python without strong polymorphism.

I do not have any problem with strong polymorphism but I think that
people tend to overstate its value. The reason that you can use file
objects and mapping objects so easily between Python modules is because
those interfaces were informally defined by Guido years ago. It seems
extremely unlikely to me that there will arise many cases where you just
happen to have an object that just happens to have the right methods
with the right parameters and semantics. This will only realistically
happen where there was an explicit or implicit interface definition that
both programmers knew about.

>..
> The distinction in my proposal is that Types (or call it interfaces if you
> like) should inherite the other way round.  When you check
> 
> a is of Type b
> 
> you should not ask a: "Are you of type b?"  You should ask b: "Do you
> include a?"

Most practically, I think that you should ask a about b AND b about a.
Then maybe you even ask a third-party registry about whether an adapter
exists to bridge from one to the other.

> In a good design, an object should be able to satisfy an infinite number of
> interfaces, most of which do not yet exist, without knowing anything about
> them.

I think that this is really only practical with some kind of adapter
layer. It does not seem realistic to expect objects to magically have
the right methods that some third party code "happens" to need. For
instance if you invent a WebBrowser object and I invent a WebBrowser
interface separately, the chances of them lining up automatically are
almost nil. But you could create an adapter that bridges from one to the
other so that conversions back and forth happen whenever necessary.

> I just glanced over this.  Is there a text description of this?  From what I
> see, it seems that you want the types be predefined.  Can the user add type
> later on and declare whatever object he has as being of this type?

Yes, you can add types whenever you want. No, you do not declare
conformance to a type. That is separate work. Michel Pelletier is
working on a conformance declaration infrastructure and so is Clark
Evans.

> > We've been discussing these issues in the type-sig. You are invited to
> > join us there.
> 
> Glad to join here.  Does Guido read here?

Sometimes. Surely not every message.

-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.activestate.com/pythoncookbook