[Grok-dev] Simple adapter question

Carsten Senger senger at rehfisch.de
Thu Mar 5 13:31:47 EST 2009

Hallo Jan,

Jan Ulrich Hasecke schrieb:
> Hash: SHA1
> Hi,
> browsing the docs I have a question concerning adapters.
> import grok
> from zope import interface
> class Fireplace(grok.Model):
>     pass
> class Cave(grok.Model):
>     pass
> class IHome(interface.Interface):
>     pass
> class Home(grok.MultiAdapter):
>     grok.adapts(Cave, Fireplace)
>     grok.implements(IHome)
>     def __init__(self, cave, fireplace):
>         self.cave = cave
>         self.fireplace = fireplace
> home = IHome(cave, fireplace)
> Why is there an IHome in the last line? I would think that
> home = Home(cave, fireplace)
> would create a home-instance.

If there is only one adapter, there is no difference. Calling the 
interface is similar to getAdapter(). Zope gets the adapter and 
instantiates it.
The purpose of calling the interface instead of the implementation class 
is to let zope choose the right adapter. This way you can have 
specialized adapters if you have more special interfaces (class 
IFloorHeatedCave(Cave)), you can mark certain objects with a specialized 
interface and you can override an adapter registration with a better 
implementation in another packages. Zope will choose the most special 
adapter for you.


More information about the Grok-dev mailing list