case insensitive dictionary

John Henry john106henry at hotmail.com
Sun Nov 26 03:00:32 EST 2006


I don't think that's sufficient.  See how many methods the author of
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/283455 had to
redefine.

Rob Williscroft wrote:
> John Henry wrote in news:1164494606.514366.124810
> @l39g2000cwd.googlegroups.com in comp.lang.python:
>
> > I believe the standard dictionary should be amened to allow the use of
> > case insensitive keys - as an option.
>
> class idict( dict ):
>
>   class __istr( str ):
>
>     def __eq__( self, other ):
>       return self.lower() == other.lower()
>
>     def __hash__( self ):
>       return self.lower().__hash__()
>
>   def __setitem__( self, k, v ):
>     dict.__setitem__( self, idict.__istr( k ), v )
>
> d = idict( a = 1, b = 2 )
> d['A'] = 3
> 
> print d
> 
> Rob.
> -- 
> http://www.victim-prime.dsl.pipex.com/




More information about the Python-list mailing list