The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Component-Oriented Design in Python (i.e.: Interfaces)

Hi,
I've been thinking of using Python for a quick prototype of a system we're considering.
The problem is that I need to demonstrate a couple of features to a development team in-house (we develop an in-house web API, that other areas build on).

At any rate, I would like to be able to use a componenent oriented structure; principally using Interfaces to define the component touch-points.
The problem is, Python doesn't seem to support interfaces (unless I've missed it somewhere in the morass of Python documentation).

I could create a abstract class and derive from that, but if theres a method of using Interfaces I'd prefer to build that in.
Does anyone know if that's possible.

Thanks.
A Potential Snake-Charmer
Wednesday, May 18, 2005
 
 
Actually you have interfaces "for free" in Python (if you mean à la Java interfaces) : check "python duck typing" in Google.

If you mean something like COM interfaces, you can also create COM components in Python (on Windows).
Pakter
Wednesday, May 18, 2005
 
 
Duck Typing sounds about right.
I'm looking for Interfaces in a Java\C# sense.

Thanks.
A Potential Snake-Charmer
Wednesday, May 18, 2005
 
 
Interfaces are not directly supported by python but you can implement your custom solution for type checking. There is one receipe in python cookbook called "Checking Whether Interfaces Are Implemented" that can be used to ensure type safety for any class. I couldn't find it on http://aspn.activestate.com/ASPN/Cookbook/Python even though it is in the paper book edition. But here's a link for links sake http://safari.oreilly.com/?x=1&mode=section&sortKey=title&sortOrder=asc&view=&g=&catid=&s=1&b=1&f=1&t=1&c=1&u=1&r=&o=1&n=1&d=1&p=1&a=0&xmlid=0596007973/pythoncook2-CHP-20-SECT-9

Just so you kow there is a pending PEP since 2001 for the same http://python.org/peps/pep-0245.html

I am not that good in python  but I guess somehow the very notion of interfaces popularized by languages like java is not very ptyhonic or so. Python is a very dynamic language, I mean you can do all sorts of introspection with the buil in functions. I was just doing some reading on descriptors, decorators & metaclasses that is some pretty powerful stuff. Heck even AOP is right there built in.
notmuch
Wednesday, May 18, 2005
 
 
Unlike Java, Python support multiple inheritance.  You can use this to do things that interfaces do, e.g.:

  class MyFileInterface(object):
      def read(self, nbytes):
          cls = self.__class__
          raise NotImplemented("%s.%s does not implement the read method" % (cls.__module__, cls.__name__)

  class XReader(SuperClass, MyFileInterface):
      def read(self, nbytes):
          return 'x' * nbytes

and later:

  assert(isinstance(f, MyFileInterface), "invalid type")

By adding metaclass and such you could get even fancier.  As an excercise fpr the reader, write code that lets you define interfaces as:

  class MyFileInterface(Interface):
      def read(self, nbytes):
          pass

but where the resulting 'read' method of 'MyFileInterface' still raises the NotImplemented exception as above.
Tim Evans Send private email
Wednesday, May 18, 2005
 
 
The whole point of Python is that you don't need interfaces. In a dynamically typed language, if you call myObj.f(), all the interpreter cares about is that myObj has a function called f at the time of the call. You do not need to implement an interface to convince the interpreter that f is really there. In fact, you can have a list of objects of all different types and iterate through them calling f(), and as long as each one has a function called f, the interpreter will be perfectly happy. This is the paradigmatic difference between static and dynamic typing.

Additionally, in Python, you can add member functions and variables to objects at runtime, so you don't need any heavy machinery to make a class factory.
John Foxx
Thursday, May 19, 2005
 
 
I should probably add: this static-vs-dynamic typing thing is beginning to take on the flavor of a holy war. Here's what I think is the bottom line:

A statically-typed language will catch a lot of errors at compile-time, but the additional safety is not free. It's not even cheap. Here's how you pay for it: every Java or C++ project begins with the design of a deep class hierarchy which is time-consuming & hard to refactor if you screw it up. Most of your classes are inheriting from other classes, not because they want to inherit anything, but because you need to inherit in order to convince the compiler that you really are an object of a certain type.

A dynamically-typed language makes that whole mess just disappear. You only need to inherit when there's something you want to inherit. You don't need do-nothing base classes (i.e. abstract classes or interfaces). The downside of this is that all errors will happen at runtime, since nothing prevents you from trying to access members that aren't there. But the upshot is, you will *get* to your unit-testing much faster because you don't have all that structure & scaffolding in your way.

Don't try to write Java in Python--invest some time in wrapping your mind around dynamic typing, then decide whether that paradigm will be helpful or harmful to your project. Hope that helps.
John Foxx
Thursday, May 19, 2005
 
 
In Python to do what u want u have Zope Interfaces[1] (u don't need to have Zope installed though) and PyProtocols[2]

Lorenzo
--
[1] http://www.zope.org/Products/ZopeInterface
[2] http://peak.telecommunity.com/PyProtocols.html
Lorenzo Bolognini Send private email
Saturday, May 21, 2005
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz