Why would a subclass of a subclass of zope.interface.Interface not inherit its parents names?
-
11-06-2021 - |
Question
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']
OTHER TIPS
Got it:
IB.names(all=True)
I guess I should check method signatures more in the future.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow