[Python-Dev] re: Sets BOF / for in dict
M.-A. Lemburg
mal@lemburg.com
Mon, 05 Feb 2001 20:45:51 +0100
Ka-Ping Yee wrote:
>
> On Mon, 5 Feb 2001, M.-A. Lemburg wrote:
> > Two things:
> >
> > 1. the proposed syntax key:value does away with the
> > easy to parse Python block statement syntax
>
> Oh, come on. Slices and dictionary literals use colons too,
> and there's nothing wrong with that. Blocks are introduced
> by a colon at the *end* of a line.
Slices and dictionary enclose the two parts in brackets -- this
places the colon into a visible context. for ... in ... : does
not provide much of a context.
> > 2. why can't we use the old 'for x,y,z in something:' syntax
> > and instead add iterators to the objects in question ?
> >
> > for key, value in object.iterator():
> > ...
>
> Because there's no good answer for "what does iterator() return?"
> in this design. (Trust me; i did think this through carefully.)
> Try it. How would you implement the iterator() method?
The .iterator() method would have to return an object which
provides an iterator API (at C level to get the best performance).
For dictionaries, this object could carry the needed state
(current position in the dictionary table) and use the PyDict_Next()
for the internals. Matrices would have to carry along more state
(one integer per dimension) and could access the internal
matrix representation directly using C functions.
This would give us: speed, flexibility and extensibility
which the syntax hacks cannot provide; e.g. how would you
specify to iterate backwards over a sequence using that notation
or diagonal for a matrix ?
> The PEP *is* suggesting that we add iterators to the objects --
> just not that we explicitly call them. In the 'for' loop you've
> written, iterator() returns a sequence, not an iterator.
No, it should return a forward iterator.
--
Marc-Andre Lemburg
______________________________________________________________________
Company: http://www.egenix.com/
Consulting: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/