conflicting __getitem__ and __iter__ orderings harmful?

Steven Taschuk staschuk at telusplanet.net
Thu May 1 15:03:57 EDT 2003


Quoth george young:
  [...]
> 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?

Not egregiously; but I think it could be made slightly easier on
your users by mimicking dicts a bit more closely:

    __{get,set,del}item__ use id as the key;

    __iter__ iterates over the ids (not the wafers!);

    itervalues iterates over the wafers.

Then the only difference between your class and a dict is that
itervalues has a guaranteed order, namely, sorted by wafer.seq.

-- 
Steven Taschuk                staschuk at telusplanet.net
"I tried to be pleasant and accommodating, but my head
 began to hurt from his banality."   -- _Seven_ (1996)





More information about the Python-list mailing list