Open In App

Python-interface module

Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction.

The package zope.interface provides an implementation of “object interfaces” for Python. It is maintained by the Zope Toolkit project. The package exports two objects, ‘Interface’ and ‘Attribute’ directly. It also exports several helper methods. It aims to provide stricter semantics and better error messages than Python’s built-in abc module.

Declaring interface

In python, interface is defined using python class statements and is a subclass of interface.Interface which is the parent interface for all interfaces.

Syntax : 
class IMyInterface(zope.interface.Interface):
    # methods and attributes

Example




import zope.interface
  
  
class MyInterface(zope.interface.Interface):
    x = zope.interface.Attribute("foo")
    def method1(self, x):
        pass
    def method2(self):
        pass
      
print(type(MyInterface))
print(MyInterface.__module__)
print(MyInterface.__name__)
  
# get attribute
x = MyInterface['x']
print(x)
print(type(x))


Output :

<class zope.interface.interface.InterfaceClass>
__main__
MyInterface
<zope.interface.interface.Attribute object at 0x00000270A8C74358>
<class 'zope.interface.interface.Attribute'>

Implementing interface

Interface acts as a blueprint for designing classes, so interfaces are implemented using implementer decorator on class. If a class implements an interface, then the instances of the class provide the interface. Objects can provide interfaces directly, in addition to what their classes implement.

Syntax : 
@zope.interface.implementer(*interfaces)
class Class_name:
    # methods

Example




import zope.interface
  
  
class MyInterface(zope.interface.Interface):
    x = zope.interface.Attribute("foo")
    def method1(self, x):
        pass
    def method2(self):
        pass
  
@zope.interface.implementer(MyInterface)
class MyClass:
    def method1(self, x):
        return x**2
    def method2(self):
        return "foo"


We declared that MyClass implements MyInterface. This means that instances of MyClass provide MyInterface.

Methods

  • implementedBy(class) – returns a boolean value, True if class implements the interface else False
  • providedBy(object) – returns a boolean value, True if object provides the interface else False
  • providedBy(class) – returns False as class does not provide interface but implements it
  • list(zope.interface.implementedBy(class)) – returns the list of interfaces implemented by a class
  • list(zope.interface.providedBy(object)) – returns the list of interfaces provided by an object.
  • list(zope.interface.providedBy(class)) – returns empty list as class does not provide interface but
    implements it.




import zope.interface
  
  
class MyInterface(zope.interface.Interface):
    x = zope.interface.Attribute('foo')
    def method1(self, x, y, z):
        pass
    def method2(self):
        pass
  
@zope.interface.implementer(MyInterface)
class MyClass:
    def method1(self, x):
        return x**2
    def method2(self):
        return "foo"
obj = MyClass()
  
# ask an interface whether it 
# is implemented by a class:
print(MyInterface.implementedBy(MyClass))
  
# MyClass does not provide 
# MyInterface but implements it:
print(MyInterface.providedBy(MyClass))
  
# ask whether an interface
# is provided by an object:
print(MyInterface.providedBy(obj))
  
# ask what interfaces are 
# implemented by a class:
print(list(zope.interface.implementedBy(MyClass)))
  
# ask what interfaces are
# provided by an object:
print(list(zope.interface.providedBy(obj)))
  
# class does not provide interface
print(list(zope.interface.providedBy(MyClass)))


Output :

True
False
True
[<InterfaceClass __main__.MyInterface>]
[<InterfaceClass __main__.MyInterface>]
[]

Interface Inheritance

Interfaces can extend other interfaces by listing the other interfaces as base interfaces.

Functions

  • extends(interface) – returns boolean value, whether one interface extends another.
  • isOrExtends(interface) – returns boolean value, whether interfaces are same or one extends another.
  • isEqualOrExtendedBy(interface) – returns boolean value, whether interfaces are same or one is extended by another.




import zope.interface
  
  
class BaseI(zope.interface.Interface):
    def m1(self, x):
        pass
    def m2(self):
        pass
  
class DerivedI(BaseI):
    def m3(self, x, y):
        pass
  
@zope.interface.implementer(DerivedI)
class cls:
    def m1(self, z):
        return z**3
    def m2(self):
        return 'foo'
    def m3(self, x, y):
        return x ^ y
      
# Get base interfaces
print(DerivedI.__bases__)
  
# Ask whether baseI extends 
# DerivedI
print(BaseI.extends(DerivedI))
  
# Ask whether baseI is equal to
# or is extended by DerivedI
print(BaseI.isEqualOrExtendedBy(DerivedI))
  
# Ask whether baseI is equal to
# or extends DerivedI
print(BaseI.isOrExtends(DerivedI))
  
# Ask whether DerivedI is equal
# to or extends BaseI
print(DerivedI.isOrExtends(DerivedI))


Output :

(<InterfaceClass __main__.BaseI>, )
False
True
False
True


Last Updated : 26 Mar, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads