question on dict subclassing and overriding __setitems__

Diez B. Roggisch deets at nospam.web.de
Tue Jun 13 19:45:38 EDT 2006


Eric S. Johansson schrieb:
> I apologize if this is an FAQ but googling has not turned up anything, 
> at least to my keywords.
> 
> I need to parse a configuration file from an existing application and 
> I'm treating it as a dictionary.  I created my class with a parent class 
> of dict.  Everything works okay except I discover I need to force keys 
> to uppercase when setting a value.
> 
> I override __setitems__ and, as you'd expect, I get a recursive loop. 
> I'm obviously missing a clue on how to break the recursion.  My 
> admittedly simpleminded method overloading looks like:
> 
>     def __setitem__ (self, index, value):
>         """force keys to uppercase"""
>         self[index.upper()] = value
           dict.__setitem__(self, index.upper()) = value

Or better even

           super(subclass, self).__setitem__(key.upper(), value)


DIez



More information about the Python-list mailing list