×

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

Global variables in python

In Python, considering the scope, variables are categorized into global and local variables. In this article, we will discuss these two types along with examples. Any variable holds a value in...

4 minutes read.

Conditional Statements in python

In this article, we will learn about conditional statements and how to apply conditional statements in Python. A decision-making statement is another name for a conditional statement. Decision-making is an important...

5 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

How to append an Array in Python

A group of objects kept at adjacent memory regions is known as an array. It is a container with a set capacity for a certain number of things, all of...

7 minutes read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python JSON Schema

Python-jsonschema JSON: JSON stands for JavaScript Object Notation. It is a text-based format that represents structured data and can be used to interchange data among various applications. This is self-defining language...

5 minutes read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

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.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.

Python Validator

Validator  The validator is a library available in the python programming language. The library in python consists of all the related modules which can be imported to perform the required operation....

3 minutes read.