conflicting __getitem__ and __iter__ orderings harmful?

george young gry at ll.mit.edu
Thu May 1 12:28:29 EDT 2003


[python 2.3a1]
I have a set of wafer objects which has a natural
ordering by a "seq" member, but each wafer also has a small
integer "id" value.

I often want to use the waferset as a sequence:
	for w in self.wafers:
		f(w)

but I also need to access individual wafers by "id" value:
	tmpwafer = self.wafers[id]

BUT, the set is not ordered by wafer.id, rather by wafer.seq.

If I implement __getitem__ and __delitem__ by id, but
__iter__ ordered by seq, am I committing heinous crimes against
python abstractions and conventions?

Is there some other clean way of providing both kinds of access?

-- George
-- 
 I cannot think why the whole bed of the ocean is
 not one solid mass of oysters, so prolific they seem. Ah,
 I am wandering! Strange how the brain controls the brain!
	-- Sherlock Holmes in "The Dying Detective"




More information about the Python-list mailing list