×

Python Interface

When creating an application, it is important to continuously keep track of its changes. As an application grows, sometimes it gets hard to manage its updates and changes. Often, you see classes which look similar but are unrelated. This leads to confusion. In this situation, we use the python interface to deal with it. It helps us to determine which class we should use. Interfaces are a very important part of software engineering.

In this tutorial, we will learn about python interfaces and how we create an interface. Also, we will see how the interface in python differs from the interface of other languages.

An interface is like a blueprint of any class. At a high level, the design of a class can be represented with the help of an interface. Similar to class, interfaces are used to define methods, but the only difference is that the method defined using interfaces are abstract. An interface defines an abstract method which then gets implemented by the class, and then these classes implement interfaces.

The implementation of an interface in python is a bit different from the other languages like Java, C, C++, etc. In these languages, we have interface keywords, while python does not have any keywords like this. Implementing an interface in python is just another way of implementing abstraction by writing a set of code.

We have a package name zope.interface, which is used to implement the interface in python. This interface provides two objects which are interface and attribute directly. It provides some helper methods too.

How do we declare the interface in python?

In python, we declare an interface using a python class statement and a subclass of interface.Interface. This class is the parent interface of all interfaces.

The syntax for creating the interface

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

Example

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

Implementing interfaces in python

We use the implementer decorator in class to implement the interface in python. Once we implement a class, the class instances provide the interface.

The syntax for implementing an interface

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

Example

import zope.interface
  
  
class Interface_name(zope.interface.Interface):
    x1 = zope.interface.Attribute("foo")
    def meth1(self, x1):
        pass
    def meth2(self):
        pass
  
@zope.interface.implementer(Interface_name)


class Class_name:
    def meth1(self, x1):
        return x1**2
    def meth2(self):
        return "foo"

Methods used for implementing an interface

  • implementedBy(class): It returns a boolean value (True or False). It gives true if the interface implements otherwise, false.
  • providedBy(class): It returns the Boolean value false. Since the class does not provide an interface, it just implements it. 
  • list(zope.interface.implementedBy(class)): It returns the list of interfaces implemented by the class.
  • list(zope.interface.providedBy(class)): It returns the list of interfaces provided by the class.
  • providedBy(object): It returns a Boolean value. It returns true if the object provides the interface. Otherwise, false.
  • list(zope.interface.providedBy(object)): It returns an empty list.

Example

import zope.interface
  
  
class Interface_name(zope.interface.Interface):
    x = zope.interface.Attribute('foo')
    def meth1(self, x, y, z):
        pass
    def meth2(self):
        pass
  
@zope.interface.implementer(Interface_name)
class Class_name:
    def meth1(self, x):
        return x**2
    def meth2(self):
        return "foo"
obj_name = Class_name()
  
# asking an interface if it is implemented by the class
print(Interface_name.implementedBy(Class_name))
  
# Class_name does not provide Interface_name but implements it:
print(Interface_name.providedBy(Class_name))
  
# asking an interface if it is provided by an object:
print(MyInterface.providedBy(obj))
  
# asking what interfaces are implemented by a class:
print(list(zope.interface.implementedBy(Class_name)))
  
# asking what interfaces are provided by an object:
print(list(zope.interface.providedBy(obj_name)))
  
# class does not provide interface
print(list(zope.interface.providedBy(Class_name)))

Output

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

Implementing interface inheritance

We can use one interface to implement other interfaces just by saving the interfaces in the list as the base interface.

Functions

  • extends(interface) – Itreturns a boolean value, whether one interface extends another.
  • isOrExtends(interface) –  Itreturns a boolean value, whether interfaces are the same or one extends another.
  • isEqualOrExtendedBy(interface) – It returns a boolean value, whether interfaces are same or another extends one.

Example

import zope.interface
  
  
class BaselistI(zope.interface.Interface):
    def meth1(self, x):
        pass
    def meth2(self):
        pass
  
class DerivedI(BaselistI):
    def meth3(self, x, y):
        pass
  
@zope.interface.implementer(DerivedI)
class class_name:
    def meth1(self, z):
        return z**3
    def meth2(self):
        return 'foo'
    def meth3(self, x, y):
        return x ^ y
      
# Get base interfaces
print(DerivedI.__bases__)
  
# Asking whether baseI inherits DerivedI
print(BaselistI.extends(DerivedI))
  
# Ask whether baselistI is equal to or is extended by DerivedI
print(BaselistI.isEqualOrExtendedBy(DerivedI))
  
# Ask whether baselistI is equal to or extends DerivedI
print(BaselistI.isOrExtends(DerivedI))
  
# Ask whether DerivedI is equal to or extends BaselistI
print(DerivedI.isOrExtends(DerivedI))

Output

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

Related Topics

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Matrix List Comprehension in Python

Introduction One of Python's most beautiful features is list comprehension. Iterating over an iterable object to create lists is a clever and succinct method. Nested List or matrix list Comprehensions, which...

6 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

How to create a login page in python

Users can access an application by providing their username and Password or by authenticating with a social media login on the login screen. Python Tkinter Python provides a variety of choices for...

3 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Sort a dataframe based on a column in Python

Sorting the dataframe based on a column requires pandas which is An open-source library called Python Pandas is described as offering high-performance data processing in Python. For both professionals and...

4 minutes read.

Python try catch exception

The try-except proclamation can deal with exceptions. Exceptions might happen when you run a program. Exceptions are blunders that occur during the execution of the program. Python won't educate you regarding...

3 minutes read.

AES CTR Python

Python Programming Language  Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

Python Abstract Class

An Introduction to Abstract Classes An Abstract Class is one of the most significant concepts of Object-Oriented Programming (OOP). An Abstract class can be deliberated as a blueprint or design for...

6 minutes read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.

Text detection using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.