[Python-Dev] Re: PEP 276 (simple iterator for ints)

Michael P. Dubner dubnerm-news at mail.ru
Mon Jul 5 23:33:28 CEST 2004


Nicolas Fleury wrote:

> Andrew Koenig wrote:
>
>>> IMO it would be clearer, and equally elegant, to write
>>> this as something like
>>>
>>>  for i in indices(myList):
>>>    ...
>>
>> I think that
>>
>> for i in myList.keys():
>>
>> would be even better, because it allow the same usage for dict and 
>> list.  Of
>> course that wouldn't generalize to other sequences that support len.
>
>
> Wouldn't be better then that indices returns the keys for a dictionary 
> instead?  It would then support all these sequences.  Maybe an index 
> is not the good term to generalize, but I don't think key is really 
> better, particularly since I would expect the use of a dictionary to 
> be more rare.
>
> Maybe another function, let's say accesses, could be used for these 
> rare cases, adding no overhead for common cases of indices:
> for key in accesses(myDict): pass
> for index in accesses(myList): pass

I'm not sure this kind of generalization is correct at all - list 
elements are sorted, but dict's doesn't.
Also accesses(myDict) might be misunderstood as accesses(myDict.keys()) 
which is incorrect in general (because keys() not guaranteed to return 
keys every time in same order).

--
Best regards,
Michael Dubner (dubnerm at mindless.com)
Brainbench MVP/HTML+JavaScript (http://www.brainbench.com)
PS: Sorry for my English



More information about the Python-Dev mailing list