Missing interfaces in Python...

Larry Bates larry.bates at websafe.com
Mon Apr 17 17:45:22 EDT 2006


redefined.horizons at gmail.com wrote:
> I'm coming from a Java background, so please don't stone me...
> 
> I see that Python is missing "interfaces". The concept of an interface
> is a key to good programming design in Java, but I've read that they
> aren't really necessary in Python. I am wondering what technique I can
> use in Python to get the same benefits to a program design that I would
> get with interfaces in Java.
> 
> For example, if I want to have a program with a Car object, and a Bus
> object. I want both of these objects to present a common group of
> methods that can be used by Mechanic objects, but slightly different
> methods that can be used by Driver objects.
> 
> In Java I would accomplish this by defining an IFixable interface that
> would be implemented by both the Car and Bus objects. Mechanic objects
> would work with any object implementing this interface.
> 
> How would I approach this problem in Python? I think I would use an
> abstract class instead of an interface for IFixable, since Python
> supports multiple inheritance, but I'm not sure this is correct.
> 
> Thanks for any suggestions.
> 
> Scott Huey
> 
Just thought I'd put in my 2 cents.  You may want to take a look at
Zope 3 (www.zope.com).  If I understand what you ware looking for, I
think they have already solved the problem (at least in one way).  It
is at least worth a quick review.

You will find that most Python programmers bristle at words like
"missing", "enforcement" and "strictly defined the type".  Python
programmers just don't work that way.  The fact that programmers in
other languages must, is their loss.

-Larry Bates




More information about the Python-list mailing list