How to implement an interface in python

In Python, the interfaces are used with the classes. Using the interface in coding means to organize and achieve the abstraction in the code. Mostly used interface is the zope.interface. it is the extracted from the Zope toolkit project. With the help of Zope toolkit, we get the interface and attributes.

We will discuss about the Interface in detail with the following methods:

  • Setting up the skeleton of Interface
  • Implementing the Interface with different methods
  • Interface inheritance using different functions

If you want to learn more about Python Programming, visit Python Programming Tutorials.

Setting up the skeleton of Interface

When we start declaring the interface, we first import the zope.interface. Then define the class for which we use the interface. In that class, we define some dummy functions with an argument of x, which is the attribute of the zope.interface to be used.

Here, we printed out the type of interface, its module that what kind of module it is, its name and at last its attribute.

import zope.interface
class Interface(zope.interface.Interface):
    x = zope.interface.Attribute("entechin")

    def method1(self, x):
        pass

    def method2(self):
        pass

print(type(Interface))
print(Interface.__module__)
print(Interface.__name__)
//printing out the attribute of the interface
x = Interface['x']
print(x)
<class 'zope.interface.interface.InterfaceClass'>
__main__
MyInterface
__main__.MyInterface.entechin

Implementing the Interface with different methods

In the following code, we just write the decorator of implementer. Then defined the class name as MyClass. In this class, the real implementation of the methods is defined. Like in method1, we just incremented the input x by 5 and In method returned the “entechin”.

import zope.interface
class Interface(zope.interface.Interface):
    x = zope.interface.Attribute("entechin")
    def method1(self, x):
        pass
    def method2(self):
        pass

@zope.interface.implementer(Interface)
class MyClass:
    def method1(self, x):
        return x+5
    def method2(self):
        return "entechin"

Now, the methods that can be used with the interface with class and its object are:

obj = MyClass()
print("Interface whether is implemented by a Class:", MyInterface.implementedBy(MyClass))
print("MyClass does not provide Interface but implements it:",Interface.providedBy(MyClass))
print("Whether an interface is provided by an object..?",Interface.providedBy(obj))
print(list("What interfaces are implemented by a class:",zope.interface.implementedBy(MyClass)))
print(list("What interfaces are provided by a class:", zope.interface.providedBy(obj)))
Interface whether is implemented by a Class: True
MyClass does not provide Interface but implements it: False
Whether an interface is provided by an object..? True
What interfaces are implemented by a class: [<InterfaceClass __main__.Interface>]
What interfaces are provided by a class: [<InterfaceClass __main__.Interface>]

Interface inheritance using different functions

In the inheritance, we made two classes. One with argument of zope.interface and second with decorator of zope.interface. We performed extend(), isOrExtends(),isEqualOrExtendedBy() functions with the classes that we made and used their method to perform different tasks.

import zope.interface
class FunctionI(zope.interface.Interface):
    def method1(self, x):
        pass
    def method2(self):
        pass

class DerivedI(FunctionI):
    def method3(self, x, y):
        pass


@zope.interface.implementer(DerivedI)
class Myclass:
    def method1(self, z):
        return z + 3

    def method2(self):
        return 'foo'

    def method3(self, x, y):
        return x ^ y

print("Base interfaces: ",DerivedI.__bases__)
print("whether baseI extends DerivedI: ",FunctionI.extends(DerivedI))
print("whether baseI is equal to or is extended by DerivedI:", FunctionI.isEqualOrExtendedBy(DerivedI))
print("Whether baseI is equal to or extends DerivedI:",FunctionI.isOrExtends(DerivedI))
print("Whether DerivedI is equal to or extends BaseI:",DerivedI.isOrExtends(DerivedI))
Base interfaces:  (<InterfaceClass __main__.FunctionI>,)
whether baseI extends DerivedI:  False
whether baseI is equal to or is extended by DerivedI True
Whether baseI is equal to or extends DerivedI False
Whether DerivedI is equal to or extends BaseI True

Leave a Comment

Your email address will not be published. Required fields are marked *