How do you specify and enforce an interface spec in Python?

An interface specification for a module as provided by languages such as C++ and Java describes the prototypes for the methods and functions of the module. Many feel that compile-time enforcement of interface specifications help in the construction of large programs. Python does not support interface specifications directly, but many of their advantages can be obtained by an appropriate test discipline for components, which can often be very easily accomplished in Python. There is also a tool, PyChecker, which can be used to find problems due to subclassing.

A good test suite for a module can at once provide a regression test and serve as both a module interface specification and a set of examples. Many Python modules can be run as a script to provide a simple “self test.” Even modules which use complex external interfaces can often be tested in isolation using trivial “stub” emulations of the external interface. The doctest and unittest modules or third-party test frameworks can be used to construct exhaustive test suites that exercise every line of code in a module.

An appropriate testing discipline can help build large complex applications in Python as well as having interface specifications would. In fact, it can be better because an interface specification cannot test certain properties of a program. For example, the append() method is expected to add new elements to the end of some internal list; an interface specification cannot test that your append() implementation will actually do this correctly, but it’s trivial to check this property in a test suite.

Writing test suites is very helpful, and you might want to design your code with an eye to making it easily tested. One increasingly popular technique, test-directed development, calls for writing parts of the test suite first, before you write any of the actual code. Of course Python allows you to be sloppy and not write test cases at all.

CATEGORY: general

 

Comment:

See also PyProtocols(http://peak.telecommunity.com/PyProtocols.html), a object adaptation framework that fufills a similar role to that of interfaces in statically typed languages.

See also PEP 245 (http://www.python.org/dev/peps/pep-0245/), which proposes an interface syntax and for Python, and PEP 246 (http://www.python.org/dev/peps/pep-0246/), which proposes semantics for an object adaptation system similar to PyProtocols. Note that neither of these PEPs have been accepted yet.

Additonally, Guido has written the following blog entires on this and related subjects: Python 3000 - Adaptation or Generic Functions? (http://www.artima.com/weblogs/viewpost.jsp?thread=155123) Interfaces or Abstract Base Classes? (http://www.artima.com/weblogs/viewpost.jsp?thread=92662) Adding Optional Static Typing to Python (http://www.artima.com/weblogs/viewpost.jsp?thread=85551) Adding Optional Static Typing to Python -- Part II (http://www.artima.com/weblogs/viewpost.jsp?thread=86641) Optional Static Typing -- Stop the Flames! (http://www.artima.com/weblogs/viewpost.jsp?thread=87182) Python Optional Typechecking Redux (http://www.artima.com/weblogs/viewpost.jsp?thread=89161)

Posted by infogami

A Django site. this page was rendered by a django application in 0.02s 2008-09-08 17:53:54.737398. hosted by webfaction.