[Grok-dev] function as named adapter: how?

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Apr 16 04:20:37 EDT 2009

Jeroen Michiel wrote:
> I want to make an named adapter, but it has to be a function.
> How do I do that in Grok?
> grok.name() can only be used on classes, and apparently, grok.adapter only
> accepts interfaces and no name='bla' params...

This work for me, but this feature has only recently been added..:

from zope.interface import Interface

class IMyInterface(Interface):

@grok.adapter(Interface, name='named')
def adaptation(context):
    print 'adapting', repr(context)
    return 'foobar' # not too return None here

>>> import grok
>>> class MyModel(grok.Model):
...     pass
>>> ctxt = MyModel()
>>> from zope.component import getAdapter
>>> adpt = getAdapter(ctxt, interface=IMyInterface, name='named')
adapting <MyModel object at ...>

>>> adpt2 = getAdapter(ctxt, interface=IMyInterface)
Traceback (most recent call last):
<InterfaceClass ...IMyInterface>, u'')

Is this what you looked for?


More information about the Grok-dev mailing list