[].keys() and [].items() (Was: Why I think range is a wart)

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Mon Mar 18 17:04:57 EST 2002


James_Althoff at i2.com <James_Althoff at i2.com> wrote:
>
>[huaiyu]
>|What I meant was that if you define items(A) to return an iterator,
>|and write a program using 'for i,x in items(A)', it will work for
>|any sequence A.  Users who supply A do not need to do anything.
>
>They have to do *something*.  They have to make sure that A supports
>whatever protocol is assumed in the implementation of the "items" function.
>That is, A *has* to be a "sequence" or whatever is assumed by items().

Well, if you assume that the domain of items(*) is the same as that of
iter(*) and that they use exactly the same protocol, then it's already
covered.

So I guess it comes down to the question of whether items() and iter() are
semantically connected in practice.  If in most cases they have the default
semantical connection, then items(A) is more convenient to A.items().  If
not, then A.items() is better as it is more flexible.

On second thought, even in the latter case, I don't see any negative point
in providing items(A) that's hooked on A.__items__.  You only need to
implement A.__items__ if the semantics is different from that derived from
iter(A).

Huaiyu



More information about the Python-list mailing list