Dictionary that adds items to itself

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu Apr 25 12:28:56 EDT 2002


On 25-Apr-2002 gyromagnetic wrote:
> Hi,
> I would appreciate some advice on the following. I would like to
> create a dictionary-like object with the same properties as a normal
> dictionary except with regard to adding items. When items are added, I
> would like them to be processed by a function that may generate
> additional items. I would like these additional items to be added to
> the dictionary, have the function applied to them, have the resulting
> items added to the dictionary, etc.
> 
> Can this be done by subclassing UserDict? Is there another way?
> 

UserDict and a couple of __foo__ methods should get it going pretty easily. 
One thing to watch out for is your function adding items forever.





More information about the Python-list mailing list