Python Equivalent to Java Interfaces?

B

Brian Kazian

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!
 
G

George Sakkis

Brian Kazian said:
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!


Check out PyProtocols (http://peak.telecommunity.com/PyProtocols.html).
Protocols in a way extend (no pun intended) typical interfaces by
giving more flexibility to independent components to talk to each
other.

George
 
T

Terry Hancock

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
 
M

Michele Simionato

Google for "Hettinger interface checking": the first hit
is the cookbook recipe you are looking for.

Michele Simionato
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top