简体   繁体   中英

Why would a subclass of a subclass of zope.interface.Interface not inherit its parents names?

Example:

>>> from zope.interface import Interface, Attribute
>>> class IA(Interface):
...   foo = Attribute("foo")
... 
>>> IA.names()
['foo']
>>> class IB(IA):
...   bar = Attribute("bar")
... 
>>> IB.names()
['bar']

How can I have IB.names() return the attributes defined in IA as well?

If you take a look at the zope.interface.interfaces module you'll find that the Interface class has a IInterface interface definition ! It documents the names method as follows:

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.
    """

To thus expand on your example:

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

Got it:

IB.names(all=True)

I guess I should check method signatures more in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM