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

119 views Asked by At

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?

2

There are 2 answers

1
Martijn Pieters On BEST ANSWER

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']
0
Ben On

Got it:

IB.names(all=True)

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