[Zope3-dev] Interface surprise

Tres Seaver tseaver@zope.com
31 Mar 2003 10:23:54 -0500


On Mon, 2003-03-31 at 10:25, Shane Hathaway wrote:
> Guido van Rossum wrote:
> >>I've been using the following idiom in Zope 2 to discover whether an 
> >>object is an interface definition:
> >>
> >>
> >>from Interface import Interface
> >>
> >>def isInterface(obj):
> >>     return Interface.isImplementedBy(obj)
> >>
> >>
> >>This idiom has a little surprise, though: any object that implements any 
> >>interface also implements Interface.  Since the above function yields 
> >>numerous false positives, I don't know the right way to detect whether 
> >>an object is an Interface.
> >>
> >>As a side effect, lots of objects promise to implement getBases(), 
> >>extends(), getDescriptionFor(), etc., but they don't.
> >>
> >>What should Zope 3 do to fix this?  Has anything been done already?
> > 
> > 
> > Can't you use issubclass(obj, Interface) in Zope3?  Seems to work for
> > me.
> 
> Maybe so.  Another thing that occurred to me after I sent the email is 
> that maybe my function should be looking for "IInterface" instead of 
> Interface, and that maybe Interface.isImplementedBy(foo) doesn't 
> actually promise anything at all.
> 
> It makes sense, but it's still surprising at first.

Right:  because all Zope3 interfaces derive from Interface, any object
which implements anything implements Interface, which is an "empty"
promise.   IInterface is the "meta-interface", which should only be
implemented by true interface objects.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com