469,625 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

classes and interfaces

hi
i come from a non OO environment. now i am learning about classes. can
i ask, in JAva, there are things like interface. eg
public interface someinterface {
public somemethod ();
....
...
}

In python , how to implement interface like the above? is it just
define a class??

class someinterface:
def somemethod: blah....

thanks

Jun 27 '06 #1
6 1669
s9************@yahoo.com wrote:
hi
i come from a non OO environment. now i am learning about classes. can
i ask, in JAva, there are things like interface. eg
public interface someinterface {
public somemethod ();
....
...
}

In python , how to implement interface like the above? is it just
define a class??


Java interfaces are a workaround the combination of static typing
(limiting polymorphism) and lack of multiple inheritance. Since Python
is dynamically typed (polymorphism does not depend on type), there's no
such need:

class Foo(object):
def doThis(self):
print "in Foo.doThis"

class Bar(object):
def doThis(self):
print "in Bar.doThis"

def doIt(obj):
obj.doThis()

f = Foo()
b = Bar()

doIt(f)
doIt(b)

A you can see, doIt() works for any object having a doThis() method. No
need for inheritance or interface here.

Note that we do have something like interfaces (in some third-part
librairies), but with a somewhat different (and much more powerful) usage:

http://peak.telecommunity.com/protocol_ref/ref.html

But if you're new to OO, this may not be the best starting point !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 27 '06 #2
<s9************@yahoo.com> wrote in message
news:11**********************@x69g2000cwx.googlegr oups.com...
hi
i come from a non OO environment. now i am learning about classes. can
i ask, in JAva, there are things like interface. eg
public interface someinterface {
public somemethod ();
....
...
}

In python , how to implement interface like the above? is it just
define a class??

class someinterface:
def somemethod: blah....

thanks


This question crops up every week or two on this list. (This is a healthy
indicator of people looking to map their Java learnings to Python. Some of
the best programming knowledge I've gained has come from comparing features
among different languages, and understanding their respective
purposes/strengths/shortcomings. In this case, presence of interfaces in
Java) Here are some recent threads that cover this topic:

http://groups.google.com/group/comp....411c8c9322821c

http://groups.google.com/group/comp....411c8c9322821c
Is this in the FAQ? Hmm, these two FAQ's may be related to your question
(although you have to know what you're looking for to recognize them):
http://www.python.org/doc/faq/genera...spec-in-python
http://www.python.org/doc/faq/progra...tatic-analysis
Lastly, you should look into
Jun 27 '06 #3
"Paul McGuire" <pt***@austin.rr._bogus_.com> wrote in message
news:ZO******************@tornado.texas.rr.com...

Lastly, you should look into


.... this blog post: http://dirtsimple.org/2004/12/python-is-not-java.html,
and its partner http://dirtsimple.org/2004/12/python-is-not-java.html.

Unfortunately, many who ask "How do I implement a Java interface in Python?"
are met with some defensiveness on this list, as if the OP is asking "What's
wrong with stupid Python language that it doesn't have something as basic as
interfaces?". Sometimes this is attitude on the OP's part, sometimes just
presumption on the part of the various readers. Some responses are of an
indignant "Interfaces? We don't need no stinkin' interfaces!" variety -
unfortunately, most of these shed more heat than light to the discussion,
usually omitting the important details as to *why* Python don't need those
malodorous code devices.

Java uses interfaces for two purposes:
1. to enforce at compile time whether a particular class implements a set of
methods, and correspondingly enforce that only instances of such classes are
permitted as arguments to functions/methods that declare arguments to be of
the interface type
2. as a workaround for not directly supporting multiple inheritance

Informally (and in design practices such as UML class diagrams), interfaces
also serve to document expected class capabilities - which in the case of
Java and C++, have the added support of compile-time checking to verify the
class contains methods corresponding to those in the interface. Note that
I've been careful *not* to imply that Java classes that pass this
compile-time check actually *implement* the interface, as is often assumed.
The only thing one knows for sure when Java accepts a class as being an
interface implementer is that the class provides all the corresponding
method definitions - but it cannot know if all expectations of behavior and
pre- and post-conditions are actually implemented within those methods.
While compile-time checking is a decent crutch 95% of the time, it will
never catch errors like this:

int passBackSomethingImportant() {
// be sure to come back and implement this before production
release!
return 0;
}

Very few "how do I implement Java interfaces in Python" posters ever get
specific about which purpose they are trying to address with their Python
interface renditions. It is entirely possible that the posters don't *know*
why the interface is there, they are just trying to directly port some Java
code to Python.

So specifically:
1. Python does not do any compile-time enforcement of method implementations
in classes of objects. While some think this is like trapeze flying without
a net, Pythoneers will reply that the net is a false assurance anyway (based
on the example I show above). So to "port" this type of interface, the only
thing to do is, well, ignore the interface - Python really doesn't require
it. You *could* create a class such as:

class ISomethingOrOther:
def whatSomethingsDo(blah, blah2):
raise NotImplementedError, "derived class failed to implement
'whatSomethingsDo' method"

and then have target classes include ISomethingOrOther in their base class
lists. But Python does not do any compile-time enforcement with these
definitions. You *still* have to do runtime testing to see if the
NotImplemented exceptions get thrown.

2. Python supports multiple inheritance without the indirection of
interfaces, so you can just inherit implementation classes directly:

class Swimmer:
def swim():
pass

class Flyer:
def fly():
pass

class Duck(Swimmer,Flyer):
pass

and merrily write:

dd = Duck()
dd.fly()
dd.swim()
Sorry for the long discourse, I didn't have time to make it shorter.

HTH,
-- Paul
Jun 27 '06 #4
s9************@yahoo.com:
>In python , how to implement interface like the above?
Interfaces are lacking in Python, but an even more generic proposal is on
its way:
http://www.artima.com/weblogs/viewpo...?thread=155123

In the mean time, interfaces have already been implemented in Zope 3:
http://www.zope.org/Products/ZopeInterface

--
René Pijlman

"To find out what you can do with interfaces, see the interface interface,
IInterface in the IInterface module."
- Comment in Zope's Interface/__init__.py
Jul 3 '06 #5
Bruno Desthuilliers:
>Java interfaces are a workaround
Troll alert.
Jul 3 '06 #6
Rene Pijlman schrieb:
Bruno Desthuilliers:
>Java interfaces are a workaround

Troll alert.
No idea how you come to that conclusion - but he is actually right with
that.

Diez
Jul 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Gomaw Beoyr | last post: by
30 posts views Thread by Frank Rizzo | last post: by
9 posts views Thread by Sean Kirkpatrick | last post: by
19 posts views Thread by dl | last post: by
6 posts views Thread by Miguel Guedes | last post: by
8 posts views Thread by =?Utf-8?B?QmVu?= | last post: by
4 posts views Thread by Peter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.