/interface

Pythonic Interface Definitions

Primary LanguagePythonApache License 2.0Apache-2.0

interface

build status

Installation

$ pip install python-interface

interface provides facilities for declaring interfaces and for statically asserting that classes implement those interfaces. It supports Python 2.7 and Python 3.4+.

interface improves on Python's abc module in two ways:

  1. Interface requirements are checked at class creation time, rather than at instance creation time. This means that interface can tell you if a class fails to meet the requirements of an interface even if you never create any instances of that class.
  2. interface requires that method signatures of interface implementations are compatible with the signatures declared in the interface. For example, the following code using abc does not produce an error:

    The equivalent code using interface produces an error indicating that the signature of our implementation method is incompatible with the signature of our interface declaration:

Defining an Interface

To define an interface, simply subclass from interface.Interface and define method stubs in your class body.

Implementing an Interface

To declare that a particular class implements an interface I, pass implements(I) as a base class for your class.