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.

providedBy(ob)

Return the interfaces provided by an object

This is the union of the interfaces directly provided by an object and interfaces implemented by it’s class.

The value returned is an IDeclaration.

implementedBy(class_)

Return the interfaces implemented for a class’ instances

The value returned is an IDeclaration.

classImplements(class_, *interfaces)

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.

Consider the following example:

class C(A, B):
   ...

classImplements(C, I1, I2)

Instances of C provide I1, I2, and whatever interfaces instances of A and B provide.

implementer(*interfaces)

Create a decorator for declaring interfaces implemented by a factory.

A callable is returned that makes an implements declaration on objects passed to it.

classImplementsOnly(class_, *interfaces)

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:

class C(A, B):
   ...

classImplements(C, IA, IB. IC)
classImplementsOnly(C. I1, I2)

Instances of C provide only I1, I2, and regardless of whatever interfaces instances of A and B implement.

implementer_only(*interfaces)

Create a decorator for declaring the only interfaces implemented

A callable is returned that makes an implements declaration on objects passed to it.

directlyProvidedBy(object)

Return the interfaces directly provided by the given object

The value returned is an IDeclaration.

directlyProvides(object, *interfaces)

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:

class C(A, B):
   ...

ob = C()
directlyProvides(ob, I1, I2)

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:

directlyProvides(ob, directlyProvidedBy(ob)-I2)

removes I2 from the interfaces directly provided by ob. The object, ob no longer directly provides I2, although it might still provide I2 if it’s class implements I2.

To add directly provided interfaces, use directlyProvidedBy and include additional interfaces. For example:

directlyProvides(ob, directlyProvidedBy(ob), I2)

adds I2 to the interfaces directly provided by ob.

alsoProvides(object, *interfaces)

Declare additional interfaces directly for an object:

alsoProvides(ob, I1)

is equivalent to:

directlyProvides(ob, directlyProvidedBy(ob), I1)
noLongerProvides(object, interface)

Remove an interface from the list of an object’s directly provided interfaces:

noLongerProvides(ob, I1)

is equivalent to:

directlyProvides(ob, directlyProvidedBy(ob) - I1)

with the exception that if I1 is an interface that is provided by ob through the class’s implementation, ValueError is raised.

implements(*interfaces)

Declare interfaces implemented by instances of a class

This function is called in a class definition (Python 2.x only).

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.

Consider the following example (Python 2.x only):

class C(A, B):
  implements(I1, I2)

Instances of C implement I1, I2, and whatever interfaces instances of A and B implement.

implementsOnly(*interfaces)

Declare the only interfaces implemented by instances of a class

This function is called in a class definition (Python 2.x only).

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.

Consider the following example (Python 2.x only):

class C(A, B):
  implementsOnly(I1, I2)

Instances of C implement I1, I2, regardless of what instances of A and B implement.

classProvides(*interfaces)

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.

provider(*interfaces)

A class decorator version of classProvides

moduleProvides(*interfaces)

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 a module. For example:

moduleImplements(I1)

is equivalent to:

directlyProvides(sys.modules[__name__], I1)
Declaration(*interfaces)

Create an interface specification

The arguments are one or more interfaces or interface specifications (IDeclaration objects).

A new interface specification (IDeclaration) with the given interfaces is returned.

Declaring The Interfaces of Objects

implementer

class zope.interface.declarations.implementer(*interfaces)[source]

Bases: object

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.declarations.implementer_only(*interfaces)[source]

Bases: object

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.

implements

(The implementer decorator is preferred to this.)

zope.interface.declarations.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

(The implementer_only decorator is preferred to this.)

zope.interface.declarations.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.

classImplementsOnly

zope.interface.declarations.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 implements
>>> 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
...
>>> class A(object):
...   implements(I3)
>>> class B(object):
...   implements(I4)
>>> 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.declarations.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.

Consider the following example:

>>> from zope.interface import Interface
>>> from zope.interface import classImplements
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> class A(object):
...   implements(I3)
>>> class B(object):
...   implements(I4)
>>> class C(A, B):
...   pass
>>> classImplements(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I3', 'I4']
>>> classImplements(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I5', 'I3', 'I4']

Instances of C provide I1, I2, I5, and whatever interfaces instances of A and B provide.

directlyProvides

zope.interface.declarations.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
...
>>> class A(object):
...     implements(IA1, IA2)
>>> class B(object):
...     implements(IB)
>>> class C(A, B):
...    implements(IC)
>>> 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.declarations.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
...
>>> class A(object):
...     implements(IA1, IA2)
>>> class B(object):
...     implements(IB)
>>> class C(A, B):
...    implements(IC)
>>> 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.declarations.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:

>>> class C(object):
...    implements(I1)
>>> 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.

classProvides

zope.interface.declarations.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.

For example:

>>> from zope.interface import Interface
>>> from zope.interface.declarations import implementer
>>> from zope.interface import classProvides
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> @implementer(IFoo)
... class C(object):
...     classProvides(IFooFactory)
>>> [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']

provider

class zope.interface.declarations.provider(*interfaces)[source]

Bases: object

Class decorator version of classProvides

moduleProvides

zope.interface.declarations.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

class zope.interface.declarations.named(name)[source]

Bases: object

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.

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.declarations.implementedByFallback(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 implements
>>> 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
...
>>> class A(object):
...   implements(I3)
>>> class B(object):
...   implements(I4)
>>> 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
...
>>> class C1(object):
...   implements(I2)
>>> class C2(C1):
...   implements(I3)
>>> [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 __builtin__.?>

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

zope.interface.declarations.providedBy()

Get an object’s interfaces

directlyProvidedBy

zope.interface.declarations.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.

class zope.interface.declarations.Declaration(*interfaces)[source]

Bases: zope.interface.interface.Specification

Interface declarations

changed(originally_changed)[source]

We, or something we depend on, have changed

flattened()[source]

Return an iterator of all included and extended interfaces

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', 'I1', 'I3', '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

zope.interface.declarations.ProvidesClass

alias of zope.interface.Provides

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.declarations.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 implementsOnly
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I31(I3): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> class A(object):
...     implements(I1)
>>> class B(object): __implemented__ = I2
...
>>> class C(A, B):
...     implements(I31)
>>> 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
>>> class COnly(A, B):
...     implementsOnly(I31)
>>> class D(COnly):
...     implements(I5)
>>> 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

Bases: object

Object Specification Descriptor

For example:

>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> @implementer(IFoo)
... class C(object):
...   classProvides(IFooFactory)
>>> [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.