As mentioned by other here:

Interfaces are not necessary in Python. This is because Python has proper multiple inheritance, and also ducktyping, which means that the places where you must have interfaces in Java, you don't have to have them in Python.

That said, there are still several uses for interfaces. Some of them are covered by Pythons Abstract Base Classes, introduced in Python 2.6. They are useful, if you want to make base classes that cannot be instantiated, but provide a specific interface or part of an implementation.

Another usage is if you somehow want to specify that an object implements a specific interface, and you can use ABC's for that too by subclassing from them. Another way is zope.interface, a module that is a part of the Zope Component Architecture, a really awesomely cool component framework. Here you don't subclass from the interfaces, but instead mark classes (or even instances) as implementing an interface. This can also be used to look up components from a component registry. Supercool!

Answer from Lennart Regebro on Stack Overflow
Top answer
1 of 8
272

As mentioned by other here:

Interfaces are not necessary in Python. This is because Python has proper multiple inheritance, and also ducktyping, which means that the places where you must have interfaces in Java, you don't have to have them in Python.

That said, there are still several uses for interfaces. Some of them are covered by Pythons Abstract Base Classes, introduced in Python 2.6. They are useful, if you want to make base classes that cannot be instantiated, but provide a specific interface or part of an implementation.

Another usage is if you somehow want to specify that an object implements a specific interface, and you can use ABC's for that too by subclassing from them. Another way is zope.interface, a module that is a part of the Zope Component Architecture, a really awesomely cool component framework. Here you don't subclass from the interfaces, but instead mark classes (or even instances) as implementing an interface. This can also be used to look up components from a component registry. Supercool!

2 of 8
239

Implementing interfaces with abstract base classes is much simpler in modern Python 3 and they serve a purpose as an interface contract for plug-in extensions.

Create the interface/abstract base class:

from abc import ABC, abstractmethod

class AccountingSystem(ABC):

    @abstractmethod
    def create_purchase_invoice(self, purchase):
        pass

    @abstractmethod
    def create_sale_invoice(self, sale):
        log.debug('Creating sale invoice', sale)

Create a normal subclass and override all abstract methods:

class GizmoAccountingSystem(AccountingSystem):

    def create_purchase_invoice(self, purchase):
        submit_to_gizmo_purchase_service(purchase)

    def create_sale_invoice(self, sale):
        super().create_sale_invoice(sale)
        submit_to_gizmo_sale_service(sale)

You can optionally have common implementation in the abstract methods as in create_sale_invoice(), calling it with super() explicitly in the subclass as above.

Instantiation of a subclass that does not implement all the abstract methods fails:

class IncompleteAccountingSystem(AccountingSystem):
    pass

>>> accounting = IncompleteAccountingSystem()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class IncompleteAccountingSystem with abstract methods
create_purchase_invoice, create_sale_invoice

You can also have abstract properties, static and class methods by combining corresponding annotations with @abstractmethod.

Abstract base classes are great for implementing plugin-based systems. All imported subclasses of a class are accessible via __subclasses__(), so if you load all classes from a plugin directory with importlib.import_module() and if they subclass the base class, you have direct access to them via __subclasses__() and you can be sure that the interface contract is enforced for all of them during instantiation.

Here's the plugin loading implementation for the AccountingSystem example above:

...
from importlib import import_module

class AccountingSystem(ABC):

    ...
    _instance = None

    @classmethod
    def instance(cls):
        if not cls._instance:
            module_name = settings.ACCOUNTING_SYSTEM_MODULE_NAME
            import_module(module_name)
            subclasses = cls.__subclasses__()
            if len(subclasses) > 1:
                raise InvalidAccountingSystemError('More than one '
                        f'accounting module: {subclasses}')
            if not subclasses or module_name not in str(subclasses[0]):
                raise InvalidAccountingSystemError('Accounting module '
                        f'{module_name} does not exist or does not '
                        'subclass AccountingSystem')
            cls._instance = subclasses[0]()
        return cls._instance

Then you can access the accounting system plugin object through the AccountingSystem class:

>>> accountingsystem = AccountingSystem.instance()

(Inspired by this PyMOTW-3 post.)

🌐
Real Python
realpython.com › python-interface
Implementing an Interface in Python – Real Python
February 21, 2024 - It doesn’t require the class that’s implementing the interface to define all of the interface’s abstract methods. ... In certain circumstances, you may not need the strict rules of a formal Python interface. Python’s dynamic nature allows you to implement an informal interface.
🌐
Tutorialspoint
tutorialspoint.com › python › python_interfaces.htm
Python - Interfaces
In languages like Java and Go, there is keyword called interface which is used to define an interface. Python doesn't have it or any similar keyword.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-interface-module
Python-interface module - GeeksforGeeks
March 26, 2020 - 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.
🌐
Medium
medium.com › @shashikantrbl123 › interfaces-and-abstract-classes-in-python-understanding-the-differences-3e5889a0746a
Interfaces and Abstract Classes in Python: Understanding the Differences | by Shashi Kant | Medium
April 10, 2023 - An interface defines a contract between a class and its users. It specifies a set of methods that a class must implement in order to be considered compatible with the interface. In Python, interfaces can be implemented using abstract base classes ...
🌐
Scaler
scaler.com › home › topics › interface in python
Interface in Python - Scaler Topics
June 23, 2024 - Interfaces aren't natively supported in Python, but abstract classes and methods offer a workaround. They are essential for software engineering and code organization. Interfaces, typically a set of method signatures in object-oriented programming, ...
🌐
Kansas State University
textbooks.cs.ksu.edu › cc410 › i-oop › 06-inheritance-polymorphism › 06-python-interfaces › index.html
Python Interfaces :: CC 410 Textbook
June 17, 2024 - The Python programming language doesn’t include direct support for interfaces in the same way as other object-oriented programming languages. However, it is possible to construct the same functionality in Python with just a little bit of work. For the full context, check out Implementing in Interface in Python from Real Python.
Find elsewhere
🌐
Chelsea Troy
chelseatroy.com › 2020 › 11 › 14 › why-use-or-not-use-interfaces-in-python
Why use (or not use) interfaces in Python? – Chelsea Troy
November 15, 2020 - An abstract class implements some ... is concerned, an interface is just a special case of abstract class that implements no methods and requires subclasses to implement all the methods....
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › interface in python
Interface in Python | How to Create Interface in Python with Examples
May 14, 2024 - Note that the interface cannot be instantiated, which means that we cannot create the object of the interface. So we use a base class to create an object, and we can say that the object implements an interface. And we will use the type function to confirm that the object implements a particular interface or not. Let see an example of python code to understand the formal interface with the general example where we are trying to create an object of the abstract class, as in the below-given code –
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python.org
discuss.python.org › ideas
Native Interface Support in Python - Ideas - Discussions on Python.org
September 22, 2023 - Dear Python Community, I would like to propose the addition of native interface support in Python to enhance code clarity, enforce contracts, and facilitate the implementation of design patterns. The introduction of interfaces as a language feature would not only improve the maintainability and understandability of Python code but also provide a mechanism for explicitly specifying contracts and multiple interface implementations in a concise and clear manner.
🌐
Python Guides
pythonguides.com › python-interface
Understand Python Interfaces
December 1, 2025 - In simple terms, a Python interface defines a contract that a class must follow. It specifies a set of methods that any implementing class must provide.
🌐
Javatpoint
javatpoint.com › interface-in-python
Interface in Python - Javatpoint
Interface in Python with tutorial, tkinter, button, overview, canvas, frame, environment set-up, first python program, etc.
🌐
Stack Abuse
stackabuse.com › guide-to-interfaces-in-python
Guide to Interfaces in Python
June 26, 2023 - They help to maintain a high level of organization, readability, and scalability in large codebases by ensuring that certain classes adhere to specific behaviors. However, it's important to understand that interfaces themselves do not contain any implementation details. They merely define the methods that need to be implemented. Now, you might be wondering, how does this concept map to Python?
🌐
Reddit
reddit.com › r/python › interfaces in python
r/Python on Reddit: Interfaces in Python
June 26, 2022 - This includes pythonic "foo_bar" -> "FooBar" naming of created objects ... You don't usually need these facilities. Therefore, if you want the runtime check, it may be easier to copy the five lines of code into an __init_subclass__ method. However, I suggest that you don't inherit from abc.ABC at all and simply · continue to decorate your methods using abc.abstractmethod, and ... Also, when implementing an interface, you may want to use the new typing.override decorator (available as typing_extensions.override).
🌐
Python.org
discuss.python.org › python help
Difference between interface and abstracts - Python Help - Discussions on Python.org
September 8, 2023 - Hi @all Could you explain what is the actual difference between interface and abstracts in python programming, what we can achieve by using two? What is the concept of oops behind the scene here? How oops concepts is …
Top answer
1 of 1
1

The type systems of Python on the one hand and C# and Java on the other hand work in a fundamentally different way.

C# and Java use a nominal type system: Two types are the same if they have the same name. Additionally, they use strong typing: assignment (and parameter passing) between references is only allowed if the target type is in the set of types formed by enumerating the source's type and declared sub-types.

Python on the other hand uses a structural type system: Two types are considered compatible if you can do the same operations on them. This is also called duck-typing (if it quacks like a duck and walks like a duck, then you can treat it as a duck).

Furthermore, type declarations for function parameters and return types are a relatively new addition to Python and they are not actively used by the Python runtime environment.

As Python doesn't require the match in type names and doesn't actively check type matches on function calls, there has traditionally been much less incentive to use interface declarations in Python than in C#/Java. And due to the structural typing, there is still no need to explicitly mention a class implements interface X. A function that is documented to need an argument of type X will accept anything that is structurally compatible with X, not just classes that explicitly mention implementing X.

Thus, interfaces in Python are useful documentation tools, but they are not needed to support Dependency Injection or Mocking in testcases, like they are in C# and Java.

🌐
GitHub
github.com › zopefoundation › zope.interface
GitHub - zopefoundation/zope.interface: Interfaces for Python
It is maintained by the Zope Toolkit project. This package provides an implementation of "object interfaces" for Python. Interfaces are a mechanism for labeling objects as conforming to a given API or contract.
Starred by 349 users
Forked by 78 users
Languages   Python 91.1% | C 8.5% | Python 91.1% | C 8.5%
🌐
DEV Community
dev.to › alvesjessica › interfaces-in-python-not-truly-an-interface-just-a-convention-43d3
Interfaces in Python: not truly an interface, just a convention. - DEV Community
September 14, 2022 - In the Python context, different from strongly typed programming languages such as Java, interface isn’t a thing. There is no interface keyword. But you can try to implement something similar to an interface. In Python we could try to represent an interface creating a kind of specialized class which would be like an abstract class that contains only abstract methods.