classes and interfaces
Bruno Desthuilliers
onurb at xiludom.gro
Tue Jun 27 07:01:47 EDT 2006
s99999999s2003 at 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 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list