例子:
>>> from zope.interface import Interface, Attribute
>>> class IA(Interface):
... foo = Attribute("foo")
...
>>> IA.names()
['foo']
>>> class IB(IA):
... bar = Attribute("bar")
...
>>> IB.names()
['bar']
如何让 IB.names() 也返回 IA 中定义的属性?
例子:
>>> from zope.interface import Interface, Attribute
>>> class IA(Interface):
... foo = Attribute("foo")
...
>>> IA.names()
['foo']
>>> class IB(IA):
... bar = Attribute("bar")
...
>>> IB.names()
['bar']
如何让 IB.names() 也返回 IA 中定义的属性?
如果你看一下这个zope.interface.interfaces
模块,你会发现这个Interface
类有一个IInterface
接口定义!它记录了names
方法如下:
def names(all=False):
"""Get the interface attribute names
Return a sequence of the names of the attributes, including
methods, included in the interface definition.
Normally, only directly defined attributes are included. If
a true positional or keyword argument is given, then
attributes defined by base classes will be included.
"""
因此扩展您的示例:
>>> from zope.interface import Interface, Attribute
>>> class IA(Interface):
... foo = Attribute("foo")
...
>>> IA.names()
['foo']
>>> class IB(IA):
... bar = Attribute("bar")
...
>>> IB.names()
['bar']
>>> IB.names(all=True)
['foo', 'bar']
知道了:
IB.names(all=True)
我想我将来应该更多地检查方法签名。