gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:58 26 4
gpt4 key购买 nike

例子:

>>> 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 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']

关于python - 为什么 zope.interface.Interface 的子类的子类不继承其父名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10822909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com