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

phil hunt philh at comuno.freeserve.co.uk
Sat Mar 16 12:42:13 EST 2002


On Thu, 14 Mar 2002 00:18:13 +0000 (UTC), Huaiyu Zhu <huaiyu at gauss.almadan.ibm.com> wrote:
>On Wed, 13 Mar 2002 22:39:02 GMT, Terry Reedy <tejarex at yahoo.com> wrote:
>>>
>>> [Clark C . Evans]
>>> | Very nice idea.  This is PEP worthy.  It is nice beacuse it
>>> | does not introduce anything new; a list can easily be viewed
>>> | as a mapping having a positive integer domain.  This is simple,
>>> | consistent, and quite elegant.
>>>
>>> I would like to see:
>>>     .keys()
>>>     .items()
>>>     .iterkeys()
>>>     .iteritems()
>>
>>I like this idea too.  Best suggestion so far for replacing
>>range(len(skjf))./
>
>Question:  Which is better:  items(x) or x.items()?

The latter, for compatibility with dictionaries. Functionality 
should be:

ar = ['a','b','c']

ar.keys()  =>  [0,1,2]
ar.items()  =>  [(0,'a'), (1,'b'), (2,'c')]

>I think items(x) can just piggy-back on the current iterator protocol, and
>can be made to work on any iteratable objects.
>
>On the other hand, x.items() has to be implemented by each and every
>list-like classes.  Their subclasses need to override them whenever
>necessary. 
>
>So else being equal, I'd prefer items(x) to x.items().

Could always have both.

-- 
<"><"><"> Philip Hunt <philh at comuno.freeserve.co.uk> <"><"><">
"I would guess that he really believes whatever is politically 
advantageous for him to believe." 
                        -- Alison Brooks, referring to Michael
                              Portillo, on soc.history.what-if



More information about the Python-list mailing list