[英]How does Python interface work (in Twisted)?
I am following this explanation , and I don't quite get how Python interpreter arrives at the following. 我正在按照这个说明进行操作 ,但我不太了解Python解释器如何到达以下位置。 In the first example, is Python seeing
@implementer(IAmericanSocket)
is not implemented by UKSocket
, then it decides to make it a AdaptToAmericanSocket
because that is the only implementation of IAmericanSocket
with one argument? 在第一个示例中,Python是否看到
@implementer(IAmericanSocket)
未由UKSocket
实现,然后它决定使其成为AdaptToAmericanSocket
因为那是IAmericanSocket
唯一带有一个参数的实现? What if there is another class instance implementing IAmericanSocket
with one argument? 如果有另一个类实例使用一个参数实现
IAmericanSocket
怎么办? In the second example, why is IAmericanSocket
not overriding AmericanSocket
's voltage method? 在第二个示例中,为什么
IAmericanSocket
不覆盖AmericanSocket
的电压方法?
>>> IAmericanSocket(uk)
<__main__.AdaptToAmericanSocket instance at 0x1a5120>
>>> IAmericanSocket(am)
<__main__.AmericanSocket instance at 0x36bff0>
with the code below: 使用以下代码:
from zope.interface import Interface, implementer
from twisted.python import components
class IAmericanSocket(Interface):
def voltage():
"""
Return the voltage produced by this socket object, as an integer.
"""
@implementer(IAmericanSocket)
class AmericanSocket:
def voltage(self):
return 120
class UKSocket:
def voltage(self):
return 240
@implementer(IAmericanSocket)
class AdaptToAmericanSocket:
def __init__(self, original):
self.original = original
def voltage(self):
return self.original.voltage() / 2
components.registerAdapter(
AdaptToAmericanSocket,
UKSocket,
IAmericanSocket)
You can see the full documentation for zope.interface here: http://docs.zope.org/zope.interface/ - it may provide a more thorough introduction than Twisted's quick tutorial. 您可以在此处查看zope.interface的完整文档: http ://docs.zope.org/zope.interface/-与Twisted的快速教程相比,它可能提供更全面的介绍。
To answer your specific question, the registerAdapter
call at the end there changes the behavior of calling IAmericanSocket
. 为了回答您的特定问题,在末尾的
registerAdapter
调用更改了调用IAmericanSocket
的行为。
When you call an Interface
, it first checks to see if its argument provides itself. 调用
Interface
,它首先检查其参数是否提供自身。 Since the class AmericanSocket
implements IAmericanSocket
, instances of AmericanSocket
provide IAmericanSocket
. 由于类
AmericanSocket
实现 IAmericanSocket
,所以AmericanSocket
实例提供了 IAmericanSocket
。 This means that when you call IAmercianSocket
with an argument of an AmericanSocket
instance, you just get the instance back. 这意味着,当您使用
AmericanSocket
实例的参数调用IAmercianSocket
时,只需将实例取回即可。
However, when the argument does not provide the interface already, the interface then searches for adapters which can convert something that the argument does provide to the target interface. 但是,当参数尚未提供接口时,接口将搜索可以将参数确实提供的内容转换为目标接口的适配器 。 ("Searches for adapters" is a huge oversimplification, but Twisted's
registerAdapter
exists specifically to allow for this type of simplification.) (“搜索适配器”是一个极大的简化,但是Twisted的
registerAdapter
专门存在以允许这种简化。)
So when IAmericanSocket
is called with an instance of a UKSocket
, it finds a registered adapter from instances of UKSocket
. 因此,当使用
IAmericanSocket
的实例调用UKSocket
,它将从UKSocket
实例中查找已注册的适配器。 The adapter itself is a 1-argument callable that takes an argument of the type being adapted "from" ( UKSocket
) and returns a value of the type being adapted "to" (provider of IAmericanSocket
). 适配器本身是一个1参数可调用该需要适于“从”类型的自变量(
UKSocket
)并返回类型的值适于“到”(提供商IAmericanSocket
)。 AdaptToAmericanSocket
is a class, but classes are themselves callable, and since its constructor takes a UKSocket
, it fits the contract of thing-that-takes-1-argument-of-type- UKSocket
-and-returns-an- IAmericanSocket
. AdaptToAmericanSocket
是一个类,但是类本身是可调用的,并且由于其构造函数采用UKSocket
,因此它适合于“以1型参数类型UKSocket
并返回IAmericanSocket
。
The existence of another class would not make a difference, unless it were registered as an adapter. 除非已将其注册为适配器,否则其他类的存在不会有所不同。 If you register two adapters which might both be suitable their interactions are complicated, but since they both do the job, in theory you shouldn't care which one gets used.
如果您注册了两个可能都适合的适配器,则它们的交互会很复杂,但是由于它们都能完成工作,因此从理论上讲,您不必在意使用哪个适配器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.