2012-05-30 2 views
1

Пример:Почему подкласс подкласса zope.interface.Interface не наследует имена своих родителей?

>>> 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() возвращают атрибуты, определенные в IA, а?

ответ

3

Если вы посмотрите на zope.interface.interfaces module, вы обнаружите, что класс Interface имеет IInterface interface definition! Это документы метод names следующим образом:

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

Чтобы таким образом расширить на вашем примере:

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

Спасибо. По какой-то причине zope.interface на docs.zope.org ушел, и я не думал проверять подпись метода до тех пор, пока не разместил вопрос. – Ben

2

Понял:

IB.names(all=True) 

Я предполагаю, что я должен проверить метод подписи больше в будущем.

Смежные вопросы