[Python-Dev] re: Using lists as sets

Moshe Zadka Moshe Zadka <mzadka@geocities.com>
Mon, 20 Mar 2000 21:23:01 +0200 (IST)


On Mon, 20 Mar 2000, Jeremy Hylton wrote:

> Yet another possibility, implemented in early versions of JPython and
> later removed, was to treat a dictionary exactly like a list: Call
> __getitem__(0), then 1, ..., until a KeyError was raised.  In other
> words, a dictionary could behave like a list provided that it had
> integer keys.

Two remarks: Jeremy meant "consecutive natural keys starting with 0",
(yes, I've managed to learn mind-reading from the timbot) and that (the
following is considered a misfeature):

import UserDict
a = UserDict.UserDict()
a[0]="hello"
a[1]="world"

for word in a:
	print word

Will print "hello", "world", and then die with KeyError.
I realize why this is happening, and realize it could only be fixed in
Py3K. However, a temporary (though not 100% backwards compatible) fix is
that "for" will catch LookupError, rather then IndexError.

Any comments?
--
Moshe Zadka <mzadka@geocities.com>. 
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com