[Tutor] Interfaces in Python
Dave Kuhlman
dkuhlman at rexx.com
Tue Jun 13 20:58:32 CEST 2006
On Tue, Jun 13, 2006 at 12:59:47PM -0400, Emily Fortuna wrote:
> Hi friends,
> I am learning Python and relating to my knowledge of Java... What is (Is
> there?) the equivalent of Java interfaces in Python? How could I write
> my own?
> Emily
>
You may be interested in this article:
http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html
And, ...
The following is more in the way of trying to keep the discussion
going than it is a real solution. Using Zope interfaces, if you
are not already using Zope, seems like more trouble than it is
worth. Still, especially for someone who is interested in
tutoring and teaching new Python programmers, possibly programmers
who are familiar with Java, Zope interfaces may be worth thinking
about.
Although this is implemented in the Zope distribution, I believe
that it is usable outside of Zope applications.
See:
http://www.zope.org/Wikis/Interfaces/FrontPage
http://svn.zope.org/Zope3/trunk/src/zope/interface/README.txt?view=markup
Here is a trivial example:
from zope.interface import Interface, implements
class IA(Interface):
def show(self, level):
"""Show this object.
"""
class A:
implements(IA)
def show(self, msg):
print '(A.show) msg: "%s"' % msg
def test():
a = A()
a.show('hello')
print IA.implementedBy(A)
test()
In order for this to work, I believe you will need:
my_zope_install/lib/python
on your PYTHONPATH.
Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list