1

例子:

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

4

2 回答 2

3

如果你看一下这个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']
于 2012-05-30T22:58:40.213 回答
2

知道了:

IB.names(all=True)

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

于 2012-05-30T19:39:00.243 回答