Interfaces in python

Thomas Weholt 2002 at weholt.org
Thu May 6 13:51:05 EDT 2004


Hi,

I'm looking for a good clean explanation on how to implement interfaces in
python. My project uses Twisted so I could use their
interface-implementation, but I'm wondering if this is a portable solution
or if it needs Twisted. What I mean is can I use my interface-dependant code
if I decide to use something else than Twisted later?

I've searched in google, and found stuff like
http://c2.com/cgi/wiki?InterfacesInPython and read the part about
interfaces, components and adapters in the Twisted-docs, but this is
allready what I'm doing now. Stuff like :

class IPerson:
    def __init__(self, name):
        self.name = name
    def say_hello(self):
        raise 'Override me in your implementation'

class Man(IPerson):
    def say_hello(self):
        return "Hello, my name is %s and I'm a man"

class Woman(IPerson):
    def say_hello(self):
        return "Hello, my name is %s and I'm a woman"

( Idiotic example I know, but it illustrates what I've been doing so far )

What I need to know is if there are other ways of doing this, better ways
which gives me more control, more features etc.

Any links to good material related to interfaces in python would be
appreciated.

Thomas





More information about the Python-list mailing list