Re: [Tutor] Interfaces in Python
Emily Fortuna wrote: Hi friends, I am learning Python and relating to my knowledge of Java... What is (Is there?) the equivalent of Java interfaces in Python? How could I write my own? Explicit, formal interfaces are used much less frequently in Python than in Java, and for beginners they are not needed at all. In Python, interfaces are often implicit and defined only by usage and documentation. Sometimes the usage is called a protocol. Because Python is dynamic, there is not need to declare the type of an object, you just use it in the desired way. This is called duck typing. http://c2.com/cgi/wiki?DuckTyping For example, the Python docs often talk about a file-like object. A file-like object is any object that implements enough of the file protocol to act like a file. In some contexts, it might be enough to implement a read() method; in other contexts, the full file protocol might be needed. The StringIO module contains a class that implements a file-like wrapper around a memory buffer. An instance of StringIO can be passed to any function that expects an open file. StringIO doesn't inherit from any interface, it just implements the needed functions. Large Python projects seem to need something more formal than this. I think Zope, Twisted and PEAK have all invented ways to formalize this notion, and one of them may become part of Python in the future. But for small projects, duck typing and implicit protocols work pretty well. I hope someone else will explain this better than me... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interfaces in Python
I am learning Python and relating to my knowledge of Java... What is (Is there?) the equivalent of Java interfaces in Python? How could I write my own? There is no direct equivalent. Interfaces are implicit in Python, they are only really necessary in statically typed languages. Dynamically typed languages rely on Duck typing and therefore the concept of an interface is not required. ( Interfaces are a serious impediment to good OO design IMHO, they imply an element of fore knowledge of how a class will be used and involve additional work that often is not needed. They are basically a flexibility point introduced to mitigate against change in a static typing environment) A good example of duck typing is the concept of a file like object which is used in several places in Python. It basically means any object which has the same message protocol as a file. This is the saqme style of OOP used in SmallTalk, Lisp and ObjectiveC (although the latter also supports statically typed interfaces too) [ It is possible to create abstract classes in Python however using some sneaky tricks but the easiest way is probably just to throw an exception in the init method! Abstract classes are an alternative way to view interfaces - interaces are cut-down abstract classes...] Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interfaces in Python
Large Python projects seem to need something more formal than this. I think Zope, Twisted and PEAK have all invented ways to formalize this notion, and one of them may become part of Python in the future. I think thats more to do with the background of the programmers involved. small projects, duck typing and implicit protocols work pretty well. Even for large projects. Lisp, SmallTalk and ObjectiveC all use duck typing and some very large projects (several 100K of code, and a few million line plus projects) have been built using those languages. The important point is that duck typing needs to be combined with exception handling and exceptions have to be written to catch the Attribute errors that result from incorrect protocol behaviour. Provided that is done duck typing is no worse than static typed interfaces, its just a different approach. I hope someone else will explain this better than me... I thought you did pretty good Kent! Alan G. (Just back from the Enterprise Architecture 2006 conference...) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Interfaces in Python
Hi friends, I am learning Python and relating to my knowledge of Java... What is (Is there?) the equivalent of Java interfaces in Python? How could I write my own? Emily ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interfaces in Python
On Tue, Jun 13, 2006 at 12:59:47PM -0400, Emily Fortuna wrote: Hi friends, I am learning Python and relating to my knowledge of Java... What is (Is there?) the equivalent of Java interfaces in Python? How could I write my own? Emily You may be interested in this article: http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html And, ... The following is more in the way of trying to keep the discussion going than it is a real solution. Using Zope interfaces, if you are not already using Zope, seems like more trouble than it is worth. Still, especially for someone who is interested in tutoring and teaching new Python programmers, possibly programmers who are familiar with Java, Zope interfaces may be worth thinking about. Although this is implemented in the Zope distribution, I believe that it is usable outside of Zope applications. See: http://www.zope.org/Wikis/Interfaces/FrontPage http://svn.zope.org/Zope3/trunk/src/zope/interface/README.txt?view=markup Here is a trivial example: from zope.interface import Interface, implements class IA(Interface): def show(self, level): Show this object. class A: implements(IA) def show(self, msg): print '(A.show) msg: %s' % msg def test(): a = A() a.show('hello') print IA.implementedBy(A) test() In order for this to work, I believe you will need: my_zope_install/lib/python on your PYTHONPATH. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor