[Interface-dev] zope.interface calling an interface with no arguments

Steve Alexander steve at canonical.com
Thu Sep 1 03:25:55 EDT 2005


> g = Graph(Default) # currently

As things stand now, when you call an interface, it uses the same
signature as 'adapt()' from PEP-246.

That is, you can say ISomething(obj) or ISomething(obj, default_value).

In the first case, an error is raised if the object cannot be adapted.
In the second case, the default value is returned if the object cannot
be adapted.

This is a nice syntax, but breaks a usability rule in that the behaviour
of an API should not change semantics when you add an optional argument.

The syntax is also limiting, because it does not allow you to express
multi-adaptation.

If we changed interfaces to allow multi-adaptation by directly calling
the interface, then many lookups in Zope 3 would read more simply:

  view = IBrowserView(context, request)

This is an example of an adaptation of two objects at once.  The factory
for this adapter takes not one argument, but two.  For example:

  class MyBrowserView:

      implements(IBrowserView)

      def __init__(self, context, request):
          ...

(This isn't quite a real example, but it gives the idea.)

What to do with default values?  This can become an argument that you
must supply using a keyword, for explicitness.  The explicitness can
sort of counteract the problem of the semantic change, although I'm
weasling around the issue.


> g = Graph() # This is what we'd like to be able to do to get an 
> instance of DefaultGraph (since it's registered as the Default for  Graph).

So, in a new world of multi-adapting syntax for calling interfaces, what
would Graph() be?

It would be an adaption of zero objects at once, which is more or less
the same as a factory.

You'd register a zero-item adapter; that is, an adapter factory from
nothing to Graph.  This factory would be called to create your Graph object.


> So far people seem to like the idea. So we're sending the idea along  to
> the Zope developers.
> 
> ]]] -- http://rdflib.net/blog/2005/08/31/zope.interface_support/

Cool.  Have you thought about following the Zope convention of tagging
interface names with a capital "I" at the start?  It sounds a bit
hungarian, but it does help a lot when reading code.

-- 
Steve Alexander


More information about the Interface-dev mailing list