Interface Implementation in Python

Goldfish gregturn at mindspring.com
Tue Mar 6 14:55:41 EST 2007


> >  I would like to know the interface concept in Python.How the
> > Interface is defined and implemented in Python?.

One way I have implemented interfaces, is as follows:

class MyInterface(object):
    def someMethod(self, argument):
        raise NotImplementedError()

If anybody ever uses that class directly, or subclasses it without
implementing a real version of that method, a runtime error can be
expected. This bridges both interfaces and abstract classes.

As others have pointed out, this isn't quite like a Java interface,
but I have found it useful to state my intentions up front, and that
is how I do it. The only way to truly enforce this is by following up
with lots of good test cases, run often.




More information about the Python-list mailing list