Interfaces in python

T

Thomas Weholt

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
 
Y

Yermat

Thomas Weholt a écrit :
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 :
[...]

Look at archive. There was already lot's of discussion about interfaces...
http://groups.google.fr/groups?q=interfaces+group:comp.lang.python

In fact, in python, you don't need interfaces !
But you can simulate if you really want them...
 
D

Doug Holton

Thomas said:
Hi,

I'm looking for a good clean explanation on how to implement interfaces in
python.

See the links to interfaces examples on this page:
http://www.python.org/cgi-bin/moinmoin/MetaClasses

Sounds like you might want to check out pyprotocols. Note though when
installing it you have to change a setting in the setup.py file manually
if you don't have a C compiler set up. It uses the C compiler if
available to optimize some things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top