Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an interface and swappable implementations in python

Tags:

python

oop

Would it be possible to create a class interface in python and various implementations of the interface.

Example: I want to create a class for pop3 access (and all methods etc.). If I go with a commercial component, I want to wrap it to adhere to a contract.

In the future, if I want to use another component or code my own, I want to be able to swap things out and not have things very tightly coupled.

Possible? I'm new to python.

like image 598
Blankman Avatar asked May 18 '10 18:05

Blankman


People also ask

Is interface possible in Python?

Unfortunately, Python doesn't have interfaces, or at least, not quite built into the language. Enter Python's abstract base class, or, cutely, ABC. Functionally, abstract base classes let you define a class with abstract methods, which all subclasses must implement in order to be initialized.

Why is there no interface in Python?

Interfaces are not natively supported by Python, although abstract classes and abstract methods can be used to go around this. At a higher perspective, an interface serves as a template for class design. Interfaces create methods in the same way that classes do, but unlike classes, these methods are abstract.

What is the purpose of interface in Python?

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. Objects can provide interfaces directly, in addition to what their classes implement.


3 Answers

For people coming from a strongly typed language background, Python does not need a class interface. You can simulate it using a base class.

class BaseAccess:
  def open(arg):
    raise NotImplementedError()

class Pop3Access(BaseAccess):
  def open(arg):
    ...

class AlternateAccess(BaseAccess):
  def open(arg):
    ...

But you can easily write the same code without using BaseAccess. Strongly typed language needs the interface for type checking during compile time. For Python, this is not necessary because everything is looked up dynamically in run time. Google 'duck typing' for its philosophy.

There is a Abstract Base Classes module added in Python 2.6. But I haven't have used it.

like image 195
Wai Yip Tung Avatar answered Oct 28 '22 23:10

Wai Yip Tung


Of course. There is no need to create a base class or an interface in this case either, as everything is dynamic.

like image 39
Yann Ramin Avatar answered Oct 28 '22 23:10

Yann Ramin


One option is to use zope interfaces. However, as was stated by Wai Yip Tung, you do not need to use interfaces to achieve the same results.

The zope.interface package is really more a tool for discovering how to interact with objects (generally within large code bases with multiple developers).

like image 44
tgray Avatar answered Oct 28 '22 23:10

tgray