[Zope-dev] improving the utility and adapter lookup APIs

Tres Seaver tseaver at palladion.com
Wed Nov 25 15:52:23 EST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Brian Sutherland wrote:
> On Wed, Nov 25, 2009 at 05:17:17PM +0100, Thomas Lotze wrote:
>> What about a simple and consistent API for all components including
>> utilities, adapters and multiadapters:
>>
>> IFoo()
>> IFoo(x)
>> IFoo(x, y)
> 
> This also doesn't allow you to use this (anti?)pattern:
> 
> class Foo:
> 
>     implements(IFoo)
> 
>     def do_something(self):
>         # custom behaviour here
>         default_foo_adapter = getAdapter(self, IFoo)
>         return default_foo_adapter.do_something() # fallback to default behaviour
> 
> It may not be very theoretically correct, but it's something I find very
> useful when writing IPublishTraverse implementing classes for
> zope.publisher to traverse over. It saves me from having to inherit from
> the default traverser.

Hmm, I may be missing something here, but if Foo implements IFoo, then
the getAdapter lookup for it will short circuit, leading you into
infinite recursion.  Except that it doesn't::

  $ bin/virtualenv-2.6 --no-site-packages /tmp/brian
  ...
  $ cd /tmp/brian
  $ bin/easy_install zope.component
  ...
  $ bin/python
  >>> from zope.interface import Interface
  >>> from zope.interface import implements
  >>> class IFoo(Interface):
  ...     pass
  ...
  >>> from zope.component import getAdapter
  >>> class Foo:
  ...     implements(IFoo)
  ...
  >>> foo = Foo()
  >>> IFoo(foo) is foo
  True
  >>> getAdapter(foo, IFoo) is foo
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
  zope.component.interfaces.ComponentLookupError: (<__main__.Foo
    instance at 0xb7d0690c>, <InterfaceClass __main__.IFoo>, u'')

which strikes me as wildly disjoint:  the IFoo behavior is "expected"
(short-circuit the lookup if the object already provides the interface),
while the getAdapter behavior is a puzzlement.


Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAksNmQIACgkQ+gerLs4ltQ7vgQCgyJqce5aMgNksSziaz8oBis1x
ZpUAoKcVmJxbIY0gHw4L39wxaV1jbW9T
=E7Cn
-----END PGP SIGNATURE-----



More information about the Zope-Dev mailing list