我想获得“C++ cast like”改编以使用来自zope.interface
. 在我的真实用例中,我使用了一个注册表,Pyramid
但它派生自zope.interface.registry.Components
,根据 changes.txt 的介绍,它能够在不依赖zope.components
. 以下示例是完整且自包含的:
from zope.interface import Interface, implements
from zope.interface.registry import Components
registry = Components()
class IA(Interface):
pass
class IB(Interface):
pass
class A(object):
implements(IA)
class B(object):
implements(IB)
def __init__(self,other):
pass
registry.registerAdapter(
factory=B,
required=[IA]
)
a = A()
b = registry.getAdapter(a,IB) # why instance of B and not B?
b = IB(A()) # how to make it work?
我想知道为什么registry.getAdapter
已经返回了适应的对象,这B
在我的例子中是一个实例。我本来希望回到课堂B
上,但也许我对适配器一词的理解是错误的。由于这一行有效,而且显然适配代码已正确注册,我也希望最后一行有效。但它失败并出现如下错误:
TypeError: ('Could not adapt', <....A object at 0x4d1c3d0>, < InterfaceClass ....IB>)
知道如何让它工作吗?