Hi,<div><br></div><div>A few weeks ago, a guy was on #python, looking to customise a dictionary to be case insensitive (he was assuming string keys). His naive implementation looked something like this:</div><div><br></div>
<div>    class CaseInsensitiveDict(dict):</div><div>        def __getitem__(self, key):</div><div>            return dict.__getitem__(self, key.lower())</div><div><br></div><div>        def __setitem__(self, key, item):<br>
            dict.__setitem__(self, key.lower(), item)</div><div><br></div><div>However he was dismayed to find that this didn't work with other methods that dict uses:</div><div><br></div><div>>>> d = CaseInsensitiveDict()</div>
<div>>>> d['a'] = 3</div><div>>>> d</div><div>{'a': 3}</div><div>>>> d['A']</div><div>3</div><div>>>> d.get('A', "No key found")</div><div>'No key found'</div>
<div><br></div><div>Eventually he was directed to dir(dict), and he seemed to accept that he would have to wrap most of the methods of the dict builtin. This seemed like the worse solution to me, and I couldn't see any real reason why python couldn't either defer to user implemented __getitem__ and __setitem__, or provide an alternative dict implementation that did allow easy customisation.</div>
<div><br></div><div>I realise that python dicts are fairly high performance structures, and that checking for a custom implementation might have an unacceptable impact for a solution to what might be seen as a minor problem. Still, I think it is worth the effort to clean up what seems to me to be a slight wart on a very fundamental type in python.</div>
<div><br></div><div>Thanks,</div><div><br></div><div>Laurie</div>