case insensitive dictionary

Rob Williscroft rtw at freenet.co.uk
Sat Nov 25 19:10:12 EST 2006


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