Declaring and Checking The Interfaces of Objects¶
Declaring what interfaces an object implements or provides, and later being able to check those, is an important part of this package. Declaring interfaces, in particular, can be done both statically at object definition time and dynamically later on.
The functionality that allows declaring and checking interfaces is
provided directly in the zope.interface
module. It is described by
the interface zope.interface.interfaces.IInterfaceDeclaration
. We
will first look at that interface, and then we will look more
carefully at each object it documents, including providing examples.
- interface zope.interface.interfaces.IInterfaceDeclaration[source]¶
Declare and check the interfaces of objects
The functions defined in this interface are used to declare the interfaces that objects provide and to query the interfaces that have been declared.
Interfaces can be declared for objects in two ways:
Interfaces are declared for instances of the object’s class
Interfaces are declared for the object directly.
The interfaces declared for an object are, therefore, the union of interfaces declared for the object directly and the interfaces declared for instances of the object’s class.
Note that we say that a class implements the interfaces provided by it’s instances. An instance can also provide interfaces directly. The interfaces provided by an object are the union of the interfaces provided directly and the interfaces implemented by the class.
Declaring The Interfaces of Objects¶
implementer¶
- class zope.interface.implementer(*interfaces)[source]¶
Declare the interfaces implemented by instances of a class.
This function is called as a class decorator.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared.
Previous declarations include declarations for base classes unless implementsOnly was used.
This function is provided for convenience. It provides a more convenient way to call
classImplements
. For example:@implementer(I1) class C(object): pass
is equivalent to calling:
classImplements(C, I1)
after the class has been created.
implementer_only¶
- class zope.interface.implementer_only(*interfaces)[source]¶
Declare the only interfaces implemented by instances of a class
This function is called as a class decorator.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).Previous declarations including declarations for base classes are overridden.
This function is provided for convenience. It provides a more convenient way to call
classImplementsOnly
. For example:@implementer_only(I1) class C(object): pass
is equivalent to calling:
classImplementsOnly(I1)
after the class has been created.
classImplementsOnly¶
- zope.interface.classImplementsOnly(cls, *interfaces)[source]¶
Declare the only interfaces implemented by instances of a class
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) replace any previous declarations.
Consider the following example:
>>> from zope.interface import implementedBy
>>> from zope.interface import implementer
>>> from zope.interface import classImplementsOnly
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> @implementer(I3)
... class A(object):
... pass
>>> @implementer(I4)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsOnly(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2']
Instances of C
provide only I1
, I2
, and regardless of
whatever interfaces instances of A
and B
implement.
classImplements¶
- zope.interface.classImplements(cls, *interfaces)[source]¶
Declare additional interfaces implemented for instances of a class
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. An effort is made to keep a consistent C3 resolution order, but this cannot be guaranteed.
Changed in version 5.0.0: Each individual interface in interfaces may be added to either the beginning or end of the list of interfaces declared for cls, based on inheritance, in order to try to maintain a consistent resolution order. Previously, all interfaces were added to the end.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import classImplements
>>> from zope.interface.ro import is_consistent
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(IA)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplements(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'IA', 'IB']
Instances of C
provide I1
and I2
, plus whatever
instances of A
and B
provide.
>>> classImplements(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I5', 'IA', 'IB']
Instances of C
now also provide I5
. Notice how I5
was
added to the end of the list of things provided directly by C
.
If we ask a class to implement an interface that extends an interface it already implements, that interface will go at the beginning of the list, in order to preserve a consistent resolution order.
>>> class I6(I5): pass
>>> class I7(IA): pass
>>> classImplements(C, I6, I7)
>>> [i.getName() for i in implementedBy(C)]
['I6', 'I1', 'I2', 'I5', 'I7', 'IA', 'IB']
>>> is_consistent(implementedBy(C))
True
This cannot be used to introduce duplicates.
>>> classImplements(C, IA, IB, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I6', 'I1', 'I2', 'I5', 'I7', 'IA', 'IB']
classImplementsFirst¶
- zope.interface.classImplementsFirst(cls, iface)[source]¶
Declare that instances of cls additionally provide iface.
The second argument is an interface or interface specification. It is added as the highest priority (first in the IRO) interface; no attempt is made to keep a consistent resolution order.
New in version 5.0.0.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import classImplements
>>> from zope.interface import classImplementsFirst
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(IA)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsFirst(C, I2)
>>> classImplementsFirst(C, I1)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'IA', 'IB']
Instances of C
provide I1
, I2
, I5
, and whatever
interfaces instances of A
and B
provide.
>>> classImplementsFirst(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I5', 'I1', 'I2', 'IA', 'IB']
Instances of C
now also provide I5
. Notice how I5
was
added to the beginning of the list of things provided directly by
C
. Unlike classImplements
, this ignores inheritance and other
factors and does not attempt to ensure a consistent resolution order.
>>> class IBA(IB, IA): pass
>>> classImplementsFirst(C, IBA)
>>> classImplementsFirst(C, IA)
>>> [i.getName() for i in implementedBy(C)]
['IA', 'IBA', 'I5', 'I1', 'I2', 'IB']
This cannot be used to introduce duplicates.
>>> len(implementedBy(C).declared)
5
>>> classImplementsFirst(C, IA)
>>> classImplementsFirst(C, IBA)
>>> classImplementsFirst(C, IA)
>>> classImplementsFirst(C, IBA)
>>> [i.getName() for i in implementedBy(C)]
['IBA', 'IA', 'I5', 'I1', 'I2', 'IB']
>>> len(implementedBy(C).declared)
5
directlyProvides¶
- zope.interface.directlyProvides(object, *interfaces)[source]¶
Declare interfaces declared directly for an object
The arguments after the object are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import providedBy
>>> from zope.interface import directlyProvides
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> @implementer(IA1, IA2)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> @implementer(IC)
... class C(A, B):
... pass
>>> ob = C()
>>> directlyProvides(ob, I1, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
The object, ob
provides I1
, I2
, and whatever interfaces
instances have been declared for instances of C
.
To remove directly provided interfaces, use directlyProvidedBy
and
subtract the unwanted interfaces. For example:
>>> from zope.interface import directlyProvidedBy
>>> directlyProvides(ob, directlyProvidedBy(ob)-I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0
removes I2
from the interfaces directly provided by ob
. The object,
ob
no longer directly provides I2
, although it might still
provide I2
if its class implements I2
.
To add directly provided interfaces, use directlyProvidedBy
and
include additional interfaces. For example:
>>> int(I2 in providedBy(ob))
0
>>> from zope.interface import directlyProvidedBy
>>> directlyProvides(ob, directlyProvidedBy(ob), I2)
adds I2
to the interfaces directly provided by ob
:
>>> int(I2 in providedBy(ob))
1
We need to avoid setting this attribute on meta classes that don’t support descriptors.
We can do away with this check when we get rid of the old EC
alsoProvides¶
- zope.interface.alsoProvides(object, *interfaces)[source]¶
Declare interfaces declared directly for an object
The arguments after the object are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to the interfaces previously declared for the object.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import alsoProvides
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> @implementer(IA1, IA2)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> @implementer(IC)
... class C(A, B):
... pass
>>> ob = C()
>>> directlyProvides(ob, I1)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
>>> alsoProvides(ob, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
The object, ob
provides I1
, I2
, and whatever interfaces
instances have been declared for instances of C
. Notice that the
alsoProvides
just extends the provided interfaces.
noLongerProvides¶
- zope.interface.noLongerProvides(object, interface)[source]¶
Removes a directly provided interface from an object.
Consider the following two interfaces:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
I1
is provided through the class, I2
is directly provided
by the object:
>>> @implementer(I1)
... class C(object):
... pass
>>> c = C()
>>> alsoProvides(c, I2)
>>> I2.providedBy(c)
True
Remove I2
from c
again:
>>> from zope.interface import noLongerProvides
>>> noLongerProvides(c, I2)
>>> I2.providedBy(c)
False
Removing an interface that is provided through the class is not possible:
>>> noLongerProvides(c, I1)
Traceback (most recent call last):
...
ValueError: Can only remove directly provided interfaces.
provider¶
For example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> from zope.interface import provider
>>> class IFooFactory(Interface):
... pass
>>> class IFoo(Interface):
... pass
>>> @implementer(IFoo)
... @provider(IFooFactory)
... class C(object):
... pass
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
Which is equivalent to:
>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> @implementer(IFoo)
... class C(object):
... pass
>>> directlyProvides(C, IFooFactory)
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']
moduleProvides¶
- zope.interface.moduleProvides(*interfaces)[source]¶
Declare interfaces provided by a module
This function is used in a module definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The given interfaces (including the interfaces in the specifications) are used to create the module’s direct-object interface specification. An error will be raised if the module already has an interface specification. In other words, it is an error to call this function more than once in a module definition.
This function is provided for convenience. It provides a more convenient way to call directlyProvides. For example:
moduleImplements(I1)
is equivalent to:
directlyProvides(sys.modules[__name__], I1)
named¶
For example:
>>> from zope.interface.declarations import named
>>> @named('foo')
... class Foo(object):
... pass
>>> Foo.__component_name__
'foo'
When registering an adapter or utility component, the registry looks for the
__component_name__
attribute and uses it, if no name was explicitly
provided.
Deprecated Functions¶
implements¶
Caution
Does not work on Python 3. Use the implementer
decorator instead.
- zope.interface.implements(*interfaces)[source]¶
Declare interfaces implemented by instances of a class
This function is called in a class definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared.
Previous declarations include declarations for base classes unless
implementsOnly
was used.This function is provided for convenience. It provides a more convenient way to call
classImplements
. For example:implements(I1)
is equivalent to calling:
classImplements(C, I1)
after the class has been created.
implementsOnly¶
Caution
Does not work on Python 3. Use the implementer_only
decorator instead.
- zope.interface.implementsOnly(*interfaces)[source]¶
Declare the only interfaces implemented by instances of a class
This function is called in a class definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).Previous declarations including declarations for base classes are overridden.
This function is provided for convenience. It provides a more convenient way to call
classImplementsOnly
. For example:implementsOnly(I1)
is equivalent to calling:
classImplementsOnly(I1)
after the class has been created.
classProvides¶
Caution
Does not work on Python 3. Use the provider
decorator instead.
- zope.interface.classProvides(*interfaces)[source]¶
Declare interfaces provided directly by a class
This function is called in a class definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The given interfaces (including the interfaces in the specifications) are used to create the class’s direct-object interface specification. An error will be raised if the module class has an direct interface specification. In other words, it is an error to call this function more than once in a class definition.
Note that the given interfaces have nothing to do with the interfaces implemented by instances of the class.
This function is provided for convenience. It provides a more convenient way to call
directlyProvides
for a class. For example:classProvides(I1)
is equivalent to calling:
directlyProvides(theclass, I1)
after the class has been created.
Querying The Interfaces Of Objects¶
All of these functions return an
IDeclaration
.
You’ll notice that an IDeclaration
is a type of
ISpecification
, as is
zope.interface.Interface
, so they share some common behaviour.
- interface zope.interface.interfaces.IDeclaration[source]¶
Extends:
zope.interface.interfaces.ISpecification
Interface declaration
Declarations are used to express the interfaces implemented by classes or provided by objects.
- __contains__(interface)¶
Test whether an interface is in the specification
Return true if the given interface is one of the interfaces in the specification and false otherwise.
- __iter__()¶
Return an iterator for the interfaces in the specification
- flattened()¶
Return an iterator of all included and extended interfaces
An iterator is returned for all interfaces either included in or extended by interfaces included in the specifications without duplicates. The interfaces are in “interface resolution order”. The interface resolution order is such that base interfaces are listed after interfaces that extend them and, otherwise, interfaces are included in the order that they were defined in the specification.
- __sub__(interfaces)¶
Create an interface specification with some interfaces excluded
The argument can be an interface or an interface specifications. The interface or interfaces given in a specification are subtracted from the interface specification.
Removing an interface that is not in the specification does not raise an error. Doing so has no effect.
Removing an interface also removes sub-interfaces of the interface.
- __add__(interfaces)¶
Create an interface specification with some interfaces added
The argument can be an interface or an interface specifications. The interface or interfaces given in a specification are added to the interface specification.
Adding an interface that is already in the specification does not raise an error. Doing so has no effect.
- __nonzero__()¶
Return a true value of the interface specification is non-empty
implementedBy¶
- zope.interface.implementedBy(cls)[source]¶
Return the interfaces implemented for a class’ instances
The value returned is an
IDeclaration
.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> from zope.interface import classImplementsOnly
>>> from zope.interface import implementedBy
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> @implementer(I3)
... class A(object):
... pass
>>> @implementer(I4)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsOnly(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2']
Instances of C
provide only I1
, I2
, and regardless of
whatever interfaces instances of A
and B
implement.
Another example:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> @implementer(I2)
... class C1(object):
... pass
>>> @implementer(I3)
... class C2(C1):
... pass
>>> [i.getName() for i in implementedBy(C2)]
['I3', 'I2']
Really, any object should be able to receive a successful answer, even an instance:
>>> class Callable(object):
... def __call__(self):
... return self
>>> implementedBy(Callable())
<implementedBy builtins.?>
Note that the name of the spec ends with a ‘?’, because the Callable
instance does not have a __name__
attribute.
This also manages storage of implementation specifications.
providedBy¶
directlyProvidedBy¶
- zope.interface.directlyProvidedBy(object)[source]¶
Return the interfaces directly provided by the given object
The value returned is an
IDeclaration
.
Classes¶
Declarations¶
Declaration objects implement the API defined by
IDeclaration
.
Exmples for Declaration.__contains__()
:
>>> from zope.interface.declarations import Declaration
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> int(I1 in spec)
0
>>> int(I2 in spec)
1
>>> int(I3 in spec)
1
>>> int(I4 in spec)
1
Exmples for Declaration.__iter__()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> i = iter(spec)
>>> [x.getName() for x in i]
['I4', 'I2', 'I3']
>>> list(i)
[]
Exmples for Declaration.flattened()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> i = spec.flattened()
>>> [x.getName() for x in i]
['I4', 'I2', 'I3', 'I1', 'Interface']
>>> list(i)
[]
Exmples for Declaration.__sub__()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration()
>>> [iface.getName() for iface in spec]
[]
>>> spec -= I1
>>> [iface.getName() for iface in spec]
[]
>>> spec -= Declaration(I1, I2)
>>> [iface.getName() for iface in spec]
[]
>>> spec = Declaration(I2, I4)
>>> [iface.getName() for iface in spec]
['I2', 'I4']
>>> [iface.getName() for iface in spec - I4]
['I2']
>>> [iface.getName() for iface in spec - I1]
['I4']
>>> [iface.getName() for iface
... in spec - Declaration(I3, I4)]
['I2']
Exmples for Declaration.__add__()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration()
>>> [iface.getName() for iface in spec]
[]
>>> [iface.getName() for iface in spec+I1]
['I1']
>>> [iface.getName() for iface in I1+spec]
['I1']
>>> spec2 = spec
>>> spec += I1
>>> [iface.getName() for iface in spec]
['I1']
>>> [iface.getName() for iface in spec2]
[]
>>> spec2 += Declaration(I3, I4)
>>> [iface.getName() for iface in spec2]
['I3', 'I4']
>>> [iface.getName() for iface in spec+spec2]
['I1', 'I3', 'I4']
>>> [iface.getName() for iface in spec2+spec]
['I3', 'I4', 'I1']
ProvidesClass¶
Descriptor semantics (via Provides.__get__
):
>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
... pass
>>> class C(object):
... pass
>>> from zope.interface.declarations import ProvidesClass
>>> C.__provides__ = ProvidesClass(C, IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> getattr(C(), '__provides__', 0)
0
Implementation Details¶
The following section discusses some implementation details and demonstrates their use. You’ll notice that they are all demonstrated using the previously-defined functions.
Provides¶
- zope.interface.Provides(*interfaces)[source]¶
Cache instance declarations
Instance declarations are shared among instances that have the same declaration. The declarations are cached in a weak value dictionary.
In the examples below, we are going to make assertions about the size of the weakvalue dictionary. For the assertions to be meaningful, we need to force garbage collection to make sure garbage objects are, indeed, removed from the system. Depending on how Python is run, we may need to make multiple calls to be sure. We provide a collect function to help with this:
>>> import gc
>>> def collect():
... for i in range(4):
... gc.collect()
>>> collect()
>>> from zope.interface import directlyProvides
>>> from zope.interface.declarations import InstanceDeclarations
>>> before = len(InstanceDeclarations)
>>> class C(object):
... pass
>>> from zope.interface import Interface
>>> class I(Interface):
... pass
>>> c1 = C()
>>> c2 = C()
>>> len(InstanceDeclarations) == before
True
>>> directlyProvides(c1, I)
>>> len(InstanceDeclarations) == before + 1
True
>>> directlyProvides(c2, I)
>>> len(InstanceDeclarations) == before + 1
True
>>> del c1
>>> collect()
>>> len(InstanceDeclarations) == before + 1
True
>>> del c2
>>> collect()
>>> len(InstanceDeclarations) == before
True
ObjectSpecification¶
- zope.interface.declarations.ObjectSpecification(direct, cls)[source]¶
Provide object specifications
These combine information for the object and for it’s classes.
For example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer_only
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I31(I3): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(I1)
... class A(object):
... pass
>>> class B(object):
... __implemented__ = I2
>>> @implementer(I31)
... class C(A, B):
... pass
>>> c = C()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I31', 'I1', 'I2']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I31', 'I3', 'I1', 'I2', 'Interface']
>>> int(I1 in providedBy(c))
1
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
0
>>> @implementer_only(I31)
... class COnly(A, B):
... pass
>>> @implementer(I5)
... class D(COnly):
... pass
>>> c = D()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I5', 'I31']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I5', 'I31', 'I3', 'Interface']
>>> int(I1 in providedBy(c))
0
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I1))
0
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
1
ObjectSpecificationDescriptor¶
- class zope.interface.declarations.ObjectSpecificationDescriptor[source]¶
Implement the
__providedBy__
attributeThe
__providedBy__
attribute computes the interfaces provided by an object.
For example:
>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> @implementer(IFoo)
... @provider(IFooFactory)
... class C(object):
... pass
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']
Get an ObjectSpecification bound to either an instance or a class, depending on how we were accessed.