Why Python does *SLICING* the way it does??

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Fri Apr 22 13:09:16 EDT 2005


Roy Smith wrote:

>>Other possibility, probably faster when almost all keys in the range are in
>>the dictionary:
>>
>>class sdict(dict):
>>    def __getitem__(self, index):
>>        if isinstance(index, slice):
>>            d = {}
>>            for key in xrange(slice.start, slice.stop, slice.step):
>>                if key in self:
>>                    d[key] = self[key]
>>            return d
>>        else:
>>            return dict.__getitem__(self, index)
> 
> The problem with that is it requires the keys to be integers.

Yes, but wasn't it thought as a replacement for a list?

Reinhold



More information about the Python-list mailing list