About iterators as a new language feature

Don Dwiggins dwig at advancedmp.net
Fri Feb 16 13:45:20 EST 2001

Grant Griffin writes:
>> Discussion went round and round for a while and moved
>> on to more general iteration constructs, prompting Ka-Ping Yee to
>> write a PEP entitled "iterators":
>> <http://python.sourceforge.net/peps/pep-0234.html>
>> Please comment!

> I went through this one, and I really like it: it seems both very
> "useful" and very "beautiful".  What more could it possibly need?

> However...

> I regret Python's increasing loss of status as "executable
> pseudo-code".  This proposal, as well as 2.0's "list comprehensions",
> seem to be making Python harder for the uninitiated to read.  (To be
> fair, though, as a "moderately-initiated" Pythoneer, I've recently
> warmed up to list comprehensions.)


> I think Python is at a crossroads where it is changing from "executable
> pseudo-code" into something increasingly...well..."complex".  If
> "growth" in a language means "new features", I guess complexity is
> inevitable.  In all honesty, the techie in me is all in favor of that. 
> But my wiser part realizes that the techie part needs to be kept in
> check.  (Or, as the great Archie Bunker said, "Stifle yourself!")

> So, give me this great new feature of iterators--if you can't manage to
> save me from myself.

I'd like to suggest an alternative approach to adding features to the
language for this kind of thing: providing "iterator-like" methods in the
"collection classes".  I'd planned to work up a message on this theme,
describing how Smalltalk uses the Block (think lambda) as a basic language
element, and builds around it "execution mechanisms" that would be language
elements in other languages, and suggesting that something like that be
considered for Python.  For example, the Smalltalk language doesn't have any
syntax for "if-else", "for", or "while" -- the equivalents are built out of
classes and methods defined in the "library", and use Blocks as an essential
enabler.  In particular, all iteration is done using methods of the
Collection hierarchy.  There's a handful of standard methods, but an
enterprising programmer can easily create new ones for special purposes.

The direction I'm looking in is to do something similar for Python.  I'm
just getting started with the language, so there are probably some issues I
can't see yet.  However, I've seen that there are some Smalltalk speakers in
this forum, so I think there could be a productive discussion on this topic.

I guess the basic theme is, wherever possible, keep the language simple but
powerful, and build new structures and mechanisms in the language, rather
than added onto it.

Don Dwiggins                    "Solvitur Ambulando"
Advanced MP Technology
dwig at advancedmp.net

More information about the Python-list mailing list