[Python-ideas] Adding some standalone iterator/sequence functions as methods of the iterator objects

Bill Janssen janssen at parc.com
Thu Aug 13 18:23:08 CEST 2009


Interesting thread.

Is Python procedural (function-oriented)?  My opinion is that it was,
when it started out, but isn't really any more, due to the growth of an
object-oriented style, in which data, state, is hidden behind an
RPC-like interface, finally officially "blessed" in Python 3 with the
appearance of abstract base classes.  In an alternate history, a more
procedural idea of Python could have grown generic functions, a la
Common Lisp.

Should the iterator interface be "wide" (lots of utility methods),
either by extending it directly or by producing a wide superclass?
Probably not.  Most languages with wide interfaces need them because
they only have "types", or they have objects, but only single
inheritance.  Java is only half-way there, with its notion of interfaces
(most of which are very wide (too wide), as well).  But in Python, you
can produce mixins, which are fully implemented partial classes,
intended to be mixed together with other classes for actual use.  So you
can produce lots of "narrow" interfaces, and mix them in as needed.
Keeping individual interfaces narrow also makes it easier to do
duck-typing.

Unfortunately, it seems to me that Python currently makes mixins
somewhat clumsy to use.  We could use a low-weight way of making a class
which is a mix of three or four mixins, much as "lambda" provides a
low-weight way of producing a small function.  Something like

    a = class(Mixin1, Mixin2, Mixin3)()
    a.mixin1_method_foo()
    a.mixin3_method_bar()

and so on.

Bill



More information about the Python-ideas mailing list