Dictionary Enhancement?

sismex01 at hebmex.com sismex01 at hebmex.com
Thu Oct 31 09:54:43 EST 2002


> From: Rich Harkins [mailto:rich at worldsinfinite.com]
> 
> I was wondering if anyone else had been wanting to enhance Python
> dictionaries such that instead of raising KeyError in the case of
> errors that another method on the dictionary object, say 
> __makeitem__ would be called to try to auto-generate an appropriate value.
>  That __makeitem__ method would then either raise KeyError itself
> or return the value to insert into the dictionary and return through
> __getitem__.
> 
> I scanned the PEP's but didn't see anything similar.
> 
> Rich
>

Well, there *is* setdefault(key, default), which is the functional
equivalent of what you're describing; if the key doesn't exist,
it's created with the default value given, and *then* returned.

-gustavo




More information about the Python-list mailing list