Alternative iterator syntax
Marcin 'Qrczak' Kowalczyk
qrczak at knm.org.pl
Wed Feb 21 16:46:33 EST 2001
Wed, 21 Feb 2001 03:32:03 -0800 (PST), Huaiyu Zhu <hzhu at users.sourceforge.net> pisze:
> Following suggestion by Jeff Petkau <jpet at eskimo.com>,
> (http://mail.python.org/pipermail/python-list/2001-February/029944.html),
> here is a different proposal for iterators.
I dislike it for the following reasons:
* It requires "magic attributes", i.e. implementing __getattr__ in
each user class that wants to be a sequence with the same interface
as list. Python already has a way used for things like obtaining
iterators: method calls. Use that. Using d.keys to mean a flavor
of d.keys() is weird.
* It is too complex. There are many special methods, and syntactic
sugar like for loops expands to one of many forms depending on
what operations are provided. Python succeeded being quite simple.
Please don't ruin that.
* The __arity__ thing does not fit dynamically typed Python at all.
When you want some static typing, go for a more general type system
(which would be optional in the context of Python) instead of
adding a kludge like this.
What do I propose instead? Choose one:
1. Design a simple iterator protocol and add methods like
xitems(), xkeys() and xvalues() which produce such iterators.
I mean simple protocol, used only for 'for' loops! A single
method for __next__ suffices (it can be __call__).
2. Design a lazy list framework. Let items(), keys(), values(),
range(), readlines() etc. return lazy lists. Laziness is not as easy
in imperative languages as in functional ones. The framework should
take care to keep the current behavior of
k = d.keys()
d[spam] = eggs
use(k)
It can be done by putting weak references (avoiding reference loops)
to iterators obtained from a mutable object inside the object, to
notify iterators when the object is about to mutate, so they suck
all elements immediately before the mutation. Perhaps design a more
general mutability notification framework before that.
--
__("< Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZASTĘPCZA
QRCZAK
More information about the Python-list
mailing list