[Zope3-dev] Re: calling interface returns object,
calling queryAdapter returns None?
Tres Seaver
tseaver at palladion.com
Wed Jan 24 20:44:31 EST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chris Withers wrote:
> Fred Drake wrote:
>> On 1/24/07, Chris Withers <chris at simplistix.co.uk> wrote:
>>> queryAdapter, for me, is "starting with the supplied object, get me
>>> something that implements the supplied interface and return None if no
>>> such object can be obtained".
>> o = IFoo(ob, None)
>> if os is not None:
>
> Ah, now that's what I was looking for, thanks :-)
Note that for regularity, I assert that the calling the IFoo interface
should allow the follwing use cases, based on the asserition that
utilities are "zero-order" adapters, just as views are "binary order"
adapters.
First the setup::
>>> from zope.interface import Interface, Attribute
>>> from zope.interface import implements, directlyProvides
>>> from zope.component import provideUtility, provideAdapter, adapts
>>> class IFoo(Interface):
... name = Attribute(u'Name')
>>> class FooUtil:
... implements(IFoo)
... def __init__(self, name):
... self.name = name
>>> fooUtil = FooUtil('default')
>>> provideUtility(fooUtil)
>>> fooUtilNamed = FooUtil('named')
>>> provideUtility(fooUtilNamed, name='named')
>>> class FooAdapter:
... implements(IFoo)
... adapts(None)
... name = 'default'
... def __init__(self, context):
... self.context = context
>>> class FooAdapterNamed(FooAdapter):
... name = 'named'
>>> provideAdapter(FooAdapter)
>>> provideAdapter(FooAdapterNamed, name='named')
Now the usage. Things which *don't* work today are marked '(SF)'.
Interfaces called with *no* context argument return
utilities, not adapters (SF)::
>>> u_default = IFoo() # return the default utility
>>> u_default.__class__.__name__, u_default.name
('FooUtil', 'default')
Named utilities can also be looked up (SF)::
>>> u_named = IFoo(name='named')
>>> u_named.__class__.__name__, u_named.name
('FooUtil', 'named')
Default adapter lookup works as expected::
>>> context = object()
>>> a_default = IFoo(context)
>>> a_default.__class__.__name__, a_default.name
('FooAdapter', 'default')
But we can also do named adapter lookup (SF)::
>>> a_named = IFoo(context, name='named')
>>> a_named.__class__.__name__, a_named.name
('FooAdapterNamed', 'named')
- --
===================================================================
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.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFuAt/+gerLs4ltQ4RAmTSAJ0SYhaKb3fPrpeIm1odC02LrbjZuQCfXK/0
NMERLbcjZsBK77OeCWUACYQ=
=wI8F
-----END PGP SIGNATURE-----
More information about the Zope3-dev
mailing list