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 に答える