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
🌐
Real Python
realpython.com › python-interface
Implementing an Interface in Python – Real Python
February 21, 2024 - This is done by classes, which then implement the interface and give concrete meaning to the interface’s abstract methods. Python’s approach to interface design is somewhat different when compared to languages like Java, Go, and C++. These languages all have an interface keyword, while Python does not.
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.)

Discussions

Classes and Implementing Interfaces
Take a look here: https://realpython.com/python-interface/ More on reddit.com
🌐 r/learnpython
10
1
February 9, 2021
Native Interface Support in Python - Ideas - Discussions on Python.org
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 ... More on discuss.python.org
🌐 discuss.python.org
1
September 22, 2023
Organising interfaces in python packages
I think from the point of view of DRY and reducing coupling, an interface ABC should live in the same module as the thing it's an interface for, e.g. the other functions and classes that use things implementing that interface. Any client module that uses "the thing it's an interface for" has to import it. It can either import the ABC at the same time, or preferably, just refer to everything on the same imported module. If that stuff is also in package 1, then that's the logical place for it in your example, but it could be deeper, or up along then down in a separate one. More on reddit.com
🌐 r/learnpython
3
2
February 4, 2022
Difference between interface and abstracts
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 getting varied in python language? Could you please guide me on getting good ... More on discuss.python.org
🌐 discuss.python.org
0
0
September 8, 2023
🌐
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 ... as Python is concerned, an interface is just a special case of abstract class that implements no methods and requires subclasses to implement all the methods....
🌐
Tutorialspoint
tutorialspoint.com › python › python_interfaces.htm
Python - Interfaces
We can create and implement interfaces in two ways − ... Formal interfaces in Python are implemented using abstract base class (ABC).
🌐
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 - For example, suppose we have a Shape interface that specifies a single method, area(). We can define the Shape interface in Python using the abc module as follows: from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def area(self): pass · Now, any class that implements the Shape interface must provide an implementation of the area() method.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-interface-module
Python-interface module - GeeksforGeeks
March 26, 2020 - 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.
Find elsewhere
🌐
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 int…
🌐
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 - When we use the Python issubclass method, it will call this method behind the scenes. See below for a discussion of what that method does. Then, each property and method in the interface is implemented as an abstract method using the @abc.abstractmethod decorator.
🌐
Medium
medium.com › towardsdev › interfaces-en-python-2a7365a9ba14
Interfaces in Python. In the context of application… | by restevean | Towards Dev
April 11, 2025 - In Python, the concept of an ... Java or C#), but it can be achieved — and is recommended — using Abstract Base Classes (ABCs) or Protocols (PEP 544)....
🌐
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 - class GameInterface: def start(self): ... as an interface but it’s not truly an interface. Once in Python there are no implements or interface keywords and we’re creating something by convention, to use the interface we created, ...
🌐
Stack Abuse
stackabuse.com › guide-to-interfaces-in-python
Guide to Interfaces in Python
June 26, 2023 - Any object that implements a specific set of methods can be treated as implementing a specific "interface". While Duck Typing provides an implicit way to mimic interface-like behavior, Python also offers a more explicit way through Abstract Base Classes.
🌐
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.
🌐
Medium
medium.com › @arnab194 › implementing-interfaces-in-python-da74b0d499c8
Implementing Interfaces in Python | by ARNAB PAL | Medium
March 2, 2023 - Here the __new__ method contains the logic for enforcing interfaces. For that, we defined a structure where all the methods signature will be defined in BaseClass . Any child class which wants to implement all the methods defined BaseClass must ...
🌐
Readthedocs
python-interface.readthedocs.io
interface — interface 1.4.0 documentation
from interface import implements, Interface class MyInterface(Interface): def method1(self, x): pass def method2(self, x, y): pass class MyClass(implements(MyInterface)): def method1(self, x): return x * 2 def method2(self, x, y): return x + y
🌐
Reddit
reddit.com › r/learnpython › organising interfaces in python packages
r/learnpython on Reddit: Organising interfaces in python packages
February 4, 2022 -

Hello everyone,

I am looking for a best practice how to organise interfaces on a packages point of view. Does somebody know a guide or has experience how to do that that.

For example.

package1
    interface_definition.py # right place?
    package1.1
        module_a
        module_a2
    package1.2
        module_b
        module_b2
    package1.3
        module_c
        module_c2
package2
    package2.1
    package2.2
    package2.3

If module_a, module_b and module_c all implementing the same abstract interface class. The concrete objects of the classes implementing the interface are used so: module_a in module_a2, module_b in module_b2 and so on.

Where would you place such an interface definition? (Interface by ABC)

Greetings Florian

🌐
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.

🌐
Quora
quora.com › Is-there-an-equivalent-to-interfaces-à-la-Java-in-Python
Is there an equivalent to interfaces (à la Java) in Python? - Quora
Answer (1 of 3): Not directly - but you can write an abstract class which defines the methods which your concrete classes need to have. You can then use multiple inheritance to ensure that your concrete class has supports the functionality you ...
🌐
Medium
ridwanray.medium.com › implement-interface-in-python-90b2a5d0397a
Implement Interface in Python. If you have had the opportunity to… | by Ridwan Yusuf | Medium
June 26, 2023 - There are several reasons why one ... the concept of interface does not come built-in python, but it can implemented using Abstract base class and inheritance....