Created
March 14, 2013 05:35
-
-
Save mxml-barmstrong/5159071 to your computer and use it in GitHub Desktop.
SliceDict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SliceDict(dict): | |
def __getitem__(self, item): | |
if isinstance(item, slice): | |
return self.get(item.start, item.stop) | |
return super(SliceDict, self).__getitem__(item) | |
>> d = SliceDict() | |
>> d['a'] = 1 | |
>> d['a'] | |
1 | |
>> d['b'] | |
KeyError: 'b' | |
>> d['b':10] | |
10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment