pythonzope.interface

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?


Solution

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