Python Equivalent to Java Interfaces?

Terry Hancock hancock at anansispaceworks.com
Sat Apr 9 01:02:02 EDT 2005


On Friday 08 April 2005 11:12 pm, Brian Kazian wrote:
> I want to insure that all subclasses implement a certain method, but could 
> not find anything that would do this for me.  Is there anyway in Python to 
> implement this check?  Thanks! 

I understand there are two interface implementations in Python, one
for Zope (in particular, the Zope 3 code has an implementation that
can be rather easily separated out if you don't want all of Zope),
and something called PyProtocols. Obviously, I only know my way
around the Zope version, but it's pretty good.

You realize of course, that if you really only wanted to check for a particular
method, you can easily do this without extra help:

if not (hasattr(myclass, mymethod) and iscallable(mymethod)):
    raise MyInvalidClassError

or something like that.  The interface modules make it easier to automate
much more complicated checks for lots of methods, etc., and are quite
useful when you have lots of interfaces to keep straight.

Cheers,
Terry


--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list