Python 如何获得;铸造式;适应与纯zope.interface一起工作?

Python 如何获得;铸造式;适应与纯zope.interface一起工作?,python,zope.interface,zope.component,Python,Zope.interface,Zope.component,我想获得“C++cast-like”的改编版本,以便使用zope.interface中的代码。在我的实际用例中,我使用的是来自Pyramid的注册表,但它源于zope.interface.registry.Components,根据changes.txt的介绍,它能够在不依赖zope.Components的情况下使用这些东西。下面的示例是完整的、自包含的: from zope.interface import Interface, implements

我想获得“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:(“无法调整”,

你知道如何使它工作吗?

要使
IB(A())
工作,你需要在
zope.interface.adapter\u hooks
列表中添加一个钩子;
IAdapterRegistry
接口有一个专用的
IAdapterRegistry.adapter\u hook
方法,我们可以用于:

from zope.interface.interface import adapter_hooks

adapter_hooks.append(registry.adapters.adapter_hook)
请参见
zope.interface
README中的

您可以使用该方法进行单适配器查找,而无需调用工厂:

from zope.interface import providedBy

adapter_factory = registry.adapters.lookup1(providedBy(a), IB)
基于您的样本:

>>> from zope.interface.interface import adapter_hooks
>>> adapter_hooks.append(registry.adapters.adapter_hook)
>>> a = A()
>>> IB(a)
<__main__.B object at 0x100721110>
>>> from zope.interface import providedBy
>>> registry.adapters.lookup1(providedBy(a), IB)
<class '__main__.B'>
>>来自zope.interface.interface导入适配器\u钩子
>>>adapter\u hook.append(registry.adapters.adapter\u hook)
>>>a=a()
>>>IB(a)
>>>从zope.interface导入提供者
>>>registry.adapters.lookup1(由(a)和IB提供)