Re: Interface & Implementation



On Fri, 12 Dec 2008 16:07:26 +0530, J Ramesh Kumar wrote:

Hi,

I am new to python. I require some help on implementing interface and
its implementation. I could not find any sample code in the web. Can you
please send me some sample code which is similar to the below java code
? Thanks in advance for your help.

............
public interface MyIfc
{
public void myMeth1();
public void myMeth2();
}
....
public class MyClass implements MyIfc {
public void myMeth1()
{
//do some implementation
}

public void myMeth2()
{
//do some implementation
}
}

There is no need for interface in python due to duck typing

class MyClass1(object):
def myMeth1(self):
# some implementation
def myMeth2(self):
# some implementation
class MyClass2(object):
def myMeth1(self):
# other implementation
def myMeth2(self):
# other implementation

cs[0] = MyClass1()
cs[1] = MyClass2()
for c in cs:
c.myMeth1()
c.myMeth2()

but if you really want it, simple inheritance might be better anyway,
though not really pythonic:

class MyIfc(object):
def myMeth1(self): return NotImplemented
def myMeth2(self): return NotImplemented
class MyClass(MyIfc):
def myMeth1(self):
# some implementation
def myMeth2(self):
# some implementation

# some might notice the (ab)use of NotImplemented sentinel there

.



Relevant Pages

  • Timeout error--newbie needs help, please
    ... I'm trying to interface with NewsGator's API, ... I'm using their sample code: ... def loadLocations ... but when I execute this (with the username, password, and ...
    (comp.lang.ruby)
  • Re: Interface & Implementation
    ... I could not find any sample code in the web. ... public void myMeth1; ... def myMeth1: ... from abc import ABCMeta, abstractmethod ...
    (comp.lang.python)
  • Re: Need some advice
    ... parts are independent from the others, except for a few interface items ... def reduction: ... > the numerical equivalent of each letter in a name and I was going to ...
    (comp.lang.python)
  • Re: A little stricter type checking
    ... def f: ... but that the interface is there at least, while in the former both steps ... Of course it is not a full replacement of a type declaration, ...
    (comp.lang.python)
  • yajb usage queries...
    ... Hi - I'm wanting to use yajb to script a fairly complex interface and I'm wondering if it's possible. ... class << tclass def rtest ...
    (comp.lang.ruby)