[Checkins] SVN: grok/trunk/doc/grok_overview.rst Wrote this a while ago, but forgot to add it.

Martijn Faassen faassen at infrae.com
Wed Dec 10 08:32:01 EST 2008


Log message for revision 93842:
  Wrote this a while ago, but forgot to add it.
  

Changed:
  U   grok/trunk/doc/grok_overview.rst

-=-
Modified: grok/trunk/doc/grok_overview.rst
===================================================================
--- grok/trunk/doc/grok_overview.rst	2008-12-10 13:14:39 UTC (rev 93841)
+++ grok/trunk/doc/grok_overview.rst	2008-12-10 13:32:00 UTC (rev 93842)
@@ -739,6 +739,29 @@
 
   myadapter = component.getAdapter(some_object, ISomeInterface)
 
+Functions as adapters
+~~~~~~~~~~~~~~~~~~~~~
+
+Sometimes an adapter doesn't need to be a full-fledged class;
+registering the factory function itself is enough. We can do this with
+the ``@grok.adapter`` and ``@grok.implementer`` decorators. This way
+we can write simple adapters that don't need to return a full-fledged
+custom class instance but for instance some built-in Python object
+like a string::
+
+  @grok.adapter(SomeClass)
+  @grok.implementer(ISomeInterface)
+  def some_interface_for_some_class(some_instance):
+      return str(some_instance)
+
+You can now do the following::
+
+  some_instance = SomeClass()
+  s = ISomeInterface(some_instance)
+  print s
+
+``ISomeInterface`` now behaves much like ``str`` for ``SomeClass``.
+
 Multi adapters
 ~~~~~~~~~~~~~~
 



More information about the Checkins mailing list