[Interface-dev] module-level interface
Stephan Richter
srichter at cosmos.phy.tufts.edu
Thu Jun 9 06:45:10 EDT 2005
On Thursday 09 June 2005 02:47, John Lenton wrote:
> Thus, in interfaces.py:
> class IWindow(Interface): pass
> class ISkin(Interface):
> Window = IWindow
> and in the skin:
> class Window(Container):
> implements(IWindow)
This does not look right to me. Interfaces can either have attributes or
methods. In your case you have a special attribute that has the requirement
to provide ``IWindow``. The right way to do it would be to write a
custom/derived implementation of ``IAttribute``. Note that Zope 3 itself has
solved this problem by providing an advanced schema package that allows you
to describe much more meta-data of attributes using fields. Here an example:
>>> import zope.interface
>>> import zope.schema
>>> class IWindow(zope.interface.Interface):
... pass
...
>>> class Window(object):
... zope.interface.implements(IWindow)
>>> class ISkin(zope.interface.Interface):
... Window = zope.schema.Object(
... schema=IWindow,
... title=u'Window',
... description=u'This is the Window of the Skin',
... required=True)
>>> class Skin(object):
... zope.interface.implements(ISkin)
... Window = None
>>> win = Window()
>>> skin = Skin()
>>> skin.Window = win
>>> import zope.interface.verify
>>> zope.interface.verify.verifyObject(IWindow, win)
True
>>> zope.interface.verify.verifyObject(ISkin, skin)
True
Note that this is working code. You can copy the above into a doctest and it
will work. Since it does not matter whether the implementations are class
instances or modules, it will work for modules as well.
Regards,
Stephan
--
Stephan Richter
CBU Physics & Chemistry (B.S.) / Tufts Physics (Ph.D. student)
Web2k - Web Software Design, Development and Training
More information about the Interface-dev
mailing list