classes and interfaces

S

s99999999s2003

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
 
B

Bruno Desthuilliers

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 !-)
 
P

Paul McGuire

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...21c?q=java+interface&rnum=20#47411c8c9322821c

http://groups.google.com/group/comp...21c?q=java+interface&rnum=20#47411c8c9322821c


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/general/#how-do-you-specify-and-enforce-an-interface-spec-in-python
http://www.python.org/doc/faq/progr...-to-help-find-bugs-or-perform-static-analysis


Lastly, you should look into
 
P

Paul McGuire

Paul McGuire said:
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
 
R

Rene Pijlman

(e-mail address removed):
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/viewpost.jsp?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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top