1

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ben
  • 2,422
  • 2
  • 16
  • 23

2 Answers2

3

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']
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. For some reason, zope.interface on docs.zope.org is gone and I didn't think to check the method signature until after I had posted the question. – Ben May 31 '12 at 13:39
2

Got it:

IB.names(all=True)

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

Ben
  • 2,422
  • 2
  • 16
  • 23