conflicting __getitem__ and __iter__ orderings harmful?

Raymond Hettinger vze4rx4y at verizon.net
Thu May 1 13:53:27 EDT 2003


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

The code smells a bit like an impending heinous
crime against a future maintainer.


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


The API for dictionaries may provide a clue.  Why not provide
__iter__() for id numbers and a separate iterseq() method that
returns an iterator over the sequence:

    for w in self.wafers.iterseq():
        f(w)

    for id in self.wafers():
        print id, self.wafer[id]


Raymond Hettinger







More information about the Python-list mailing list