Interface and duck typing woes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Aug 29 22:42:33 EDT 2013


On Thu, 29 Aug 2013 09:40:32 -0300, Joe Junior wrote:

> Well, the main reason for me asking this question here was because of
> the Java/C#/Whatever developer in me craving for an Interface for the
> container's items, and I noticed that I'm not alone in this. But I was
> actually expecting the "We're all consenting adults, here", I guess I
> just needed the confirmation :-)
> 
> Another reason for this question is that I read some people saying they
> wouldn't use python for large projects, and they always point at the
> lack of Interfaces as a concern. I actually disagree, but I can see
> their point. What do you think?

Interfaces aren't a built-in part of the language, but big frameworks 
like Zope and Twisted include them. See for example discussion here:

http://dirtsimple.org/2004/12/python-interfaces-are-not-java.html

In a more ad-hoc manner, there are recipes for interface-like 
functionality. For example, from the Python Cookbook, we have this:

http://code.activestate.com/recipes/52291


It's a myth that Python is entirely opposed to type-checking. Many built-
ins do it. Way back in Python 1.5, Python's creator Guido van Rossum 
wrote an essay describing a way to implement Eiffel-like pre- and post-
condition checking:

http://www.python.org/doc/essays/metaclasses/


These days, it would be relatively simple to implement pre- and post-
condition checking using decorators, and indeed one of the motivating use-
cases for function annotations in Python 3 is to allow such things.

http://www.python.org/dev/peps/pep-3107/

(Function annotations are perhaps the best Python feature that nobody 
uses.)


-- 
Steven



More information about the Python-list mailing list