[Zope3-Users] Checking if an interface is provided by an object or could be adapted to

Tom Dossis td at yoma.com.au
Sun Apr 30 18:28:43 EDT 2006


Martin Aspeli wrote:
> Hi,
> 
> I'm probably doing something very wrong, but ...
> 
> I have a general interface that can either by directly provided by an  
> object or class (typically with implements() on the class), but could 
> also  be provided via an adapter. I have a few places where I just need 
> to check  for this (it's not really a marker interface, but I need to 
> know whether  other code later on would be able to use the interface or 
> not).
> 
> In my tests, I tried to use queryAdapter(), but that didn't return  
> anything when the interface was implemented directly by the class.  
> Similarly, Interface.providedBy() returns (expectedly) False if the  
> interface could only be obtained via an adapter. Currently I do:
> 
>       try:
>             adapted = IFoo(context)
>             return True
>         except (ComponentLookupError, TypeError):
>             return False
> 
> but that's just silly... Is there a pattern for this?

adapted = IFoo(conext, None):
if adapted is not None:
   ...



More information about the Zope3-users mailing list