By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Python Equivalent to Java Interfaces?

P: n/a
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!
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
"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!

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

Jul 18 '05 #3

P: n/a
There's 4 places to look: zope, twisted, PEAK and pyProtocols: this
link describes pyprotocols pretty well

http://peak.telecommunity.com/protoc...protocols.html
http://peak.telecommunity.com/PyProtocols.html

Jul 18 '05 #4

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

Michele Simionato

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.