conflicting __getitem__ and __iter__ orderings harmful?

Michael Chermside mcherm at mcherm.com
Thu May 1 14:29:57 EDT 2003


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

No, it's not a heinous crime. In fact, Python is pretty good about
allowing you to violate all kinds of conventions IF you really
want to. But I wouldn't bother in this case. Seems to me that
what you need is TWO different iterators for your container,
one that goes by seq and one that goes by id.

I suggest that __iter__() give the iterator that's sorted by
id (to match __getitem__), and iterSeq() give the iterator that's
sorted by seq. That's a lot like the way that dict works.

After all, TMTOWTSI.

There's-More-Than-One-Way-To-Sort-It ly yours,

-- Michael Chermside






More information about the Python-list mailing list