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

Nicolas Fleury nidoizo at yahoo.com
Mon Jul 5 00:07:25 CEST 2004


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

But since iterating through a dictionary means to iterate through the 
keys, while it means to iterate through the elements for a list, would 
that be really uniform to support generalization with dictionaries?  Is 
it useful?

Regards,
Nicolas




More information about the Python-Dev mailing list