为什么zope.interface.Interface的子类的子类不会继承其父类名?

时间:2012-05-30 19:24:55

标签: python zope.interface

示例:

>>> 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中定义的属性?

2 个答案:

答案 0 :(得分:3)

如果您查看zope.interface.interfaces module,您会发现Interface班级有IInterface interface definition!它记录了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']

答案 1 :(得分:2)

知道了:

IB.names(all=True)

我想我将来应该更多地检查方法签名。