[Twisted-Python] Interfaces and adapters

en.karpachov at ospaz.ru en.karpachov at ospaz.ru
Sat Aug 27 05:09:21 EDT 2005


On Fri, 26 Aug 2005 23:22:38 -0400
glyph at divmod.com wrote:

> Adaptation is slow enough as it is.  Enforcing this convention would be a fairly substantial cost for something which is already multiplying the cost of what would normally be a function call by tens or hundreds.
> 
> Not enforcing it is also in the spirit of Python's "language for consenting adults".  If your object really doesn't implement the interface, the unit tests will catch it, right? ;-)

For an object I own, yes, that's right. For someone else's objects, I'm not
sure how to deal with them.

The question is, if the object doesn't declare itself provides the
interface it adapts to, should this be considered as a bug? If so, then we
have it with twisted.web.server.Session and nevow.guard.GuardSession (and
probably the same situation with twisted.web2).

There is another problem with it. The twisted.web.server.Session cannot
declare itself implementing nevow.inevow.ISession as it doesn't know
anything about nevow; but nevow registers it as default adapter from
WovenContext to inevow.ISession. So we have a situation:

    session = inevow.ISession(ctx)
    assert inevow.ISession.providedBy(sess) # oops! it is not

What use in the adaptation above then? It's not really an adapter to the
interface we need but just some arbitrary function call, not related to
interfaces at all, incidentally looking like an adapter.

Probably, a decorator to the adapterFactory could help; even if all got a
bit slower, we'll get more correct behaviour. Anyway, is it that slower?
I've heard the nevow as well as Python are not about speed, but about
building robust applications.

-- 
jk




More information about the Twisted-Python mailing list