[Zope3-dev] Re: calling interface returns object,
calling queryAdapter returns None?
Chris Withers
chris at simplistix.co.uk
Thu Jan 25 05:13:59 EST 2007
All the following are great, especially the (SF) ones ;-)
Tres Seaver wrote:
> >>> 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,*args):
> ... 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')
I'll note that none of these appear to do contextual lookup.
Would we want a spelling like this which worked with contextual lookup?
One thing I think I mentioned before would be the ability to use
"calling the interface" to get a multi-adapter:
>>> provideAdapter(FooAdapter,adapts=(None,None))
>>> a_default = IFoo(objects=(context,context))
>>> a_default.__class__.__name__, a_default.name
('FooAdapter', 'default')
One other thing, where is the code that gets called when you do
ISomething(..etc...)? I had a look in interface.py and couldn't find
anything :-S
I'd be interested in knocking the above set of extras up on a branch and
seeing what people think...
cheers,
Chris
--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk
More information about the Zope3-dev
mailing list