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 -- http://mail.python.org/mailman/listinfo/python-list