Creating Interfaces in Python
While working on our project for software construction today, my partner and I started to work out an implementation of interfaces in Python. Fortunately, we didn’t end up needing to go to this extreme (it’s a weekend assignment). Interfaces have been proposed for python before, but the changes were never made. Anyway, we worked out a basic strategy to implement interfaces in python. The most common approach for this sort of thing is to just make a base class with methods that raise the NotImplementedError if they should be overridden. For the most basic implementation of an interface, this approach works, but what if we want to put contracts or a docstring test on this method? There is essentially no way to go about such an implementation with standard methods in python. Instead of using the traditional syntax to express inheritance, some function would be needed to implement interfaces. Really, most of we want to get from the interface could be considered “shell” around another function. We just want to put in the guts part of the method, and if the guts are not put in, python should raise an error. The easiest way to implement something like that would be to just maintain two methods. So, for every method foobar(self, arg1, arg2, …), there would also be a function __foobar(self, arg1, arg2, ….). Perhaps a different convention, such as __Interface_foobar(self, arg1, arg2, …), would be appropriate as to not interfere with one’s ability to assign __foobar to something else, but these are points are trivial. Ideally, I will come up with some sort of nice interface metaclass or base class and a set of function decorators that would take care of much of the work involved, but written out if full, it would look like
class Foobar(Interface):
#===============================================================================
def __init__(self):
raise NotImplementedError, "__init__ must be implemented by a subclass"
#===============================================================================
def foobar(self, aArg):
"""
This is a function is the shell of an interface method
"""
if not isinstance(aArg, BarType):
raise TypeError, "aArg must be a BarType"
return self.__Interface_foobar(aArg)
#===============================================================================
def spam(self):
"""
Just some regular method.
"""
return 3
Then after defining some class that implements this interface Foobar, do something like this
Foobar.implementedBy(eggs)
I will probably have to make some changes once I start banging away at python. I know python well, but I don’t know every bit of trivia.
One of the consequences of this setup is that everything is done at runtime, and some external testing procedure would be needed to verify that everything follows the interface. On the plus side, these would be something unique from interfaces as well; these thing really could be used for a whole bunch of things beyond the scope of normal interface. Also, everything needed to implement this sort of thing could be done without any changes to the language itself or use of modules outside of the python standard library. So, I will work on this thing sometime when I have time, which is never…
Posted in Python, coding, design | No Comments


