您需要在类上或注册表中提供上下文。
我怀疑您没有传达整个问题集——适配器是一个组件,它适应指定接口的对象,并提供另一个。您的示例未能指定要适应的上下文是什么,也就是说,在构建适配器对象时,它的类适应了什么样的对象?
例如,这很好用:
from zope.interface import Interface, implements
from zope.component import getGlobalSiteManager, adapts
class IWeight(Interface):
pass
class IVolume(Interface):
pass
class WeightToVolume(object):
implements(IVolume)
adapts(IWeight)
#...
gsm = getGlobalSiteManager()
gsm.registerAdapter(WeightToVolume)
虽然您可以为此使用装饰器(实现器/适配器)语法,但按照惯例,对于作为类而不是函数的适配器工厂,最好使用实现/适配器。
至少,如果您的适配器没有声明它在类或工厂函数本身上适应的内容,您将需要告诉注册表。在最广泛的情况下,这可能看起来像:
gsm.registerAdapter(MyAdapterClassHere, required=(Interface,))
当然,上面这个例子是一个声称可以适应任何上下文的适配器,除非你知道为什么需要它,否则不建议这样做。