missing? dictionary methods

George Sakkis gsakkis at rutgers.edu
Mon Mar 21 04:00:26 EST 2005


"Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message
news:slrnd3t10j.20i.apardon at rcpc42.vub.ac.be...
> Well at least I find them missing.
>
> For the moment I frequently come across the following cases.
>
> 1) Two files, each with key-value pairs for the same dictionary.
> However it is an error if the second file contains a key that
> was not in the first file.
>
> In treating the second file I miss a 'set' method.
> dct.set(key, value) would be equivallent to dct[key] = value,
> except that it would raise a KeyError if the key wasn't
> already in the dictionary.
>
>
> 2) One file with key-value pairs. However it is an error
> if a key is duplicated in the file.
>
> In treating such files I miss a 'make' method.
> dct.make(key, value) would be equivallent to dct[key] = value.
> except that it would raise a KeyError if the key was
> already in the dictionary.
>
>
> What do other people think about this?
>
> -- 
> Antoon Pardon

+1

I'm sure I've needed and implemented this functionality in the past, but it was simple enough to
even think of extracting them into functions/methods. In contrast to the recent pre-PEP about dict
accumulating methods, set() and make() (or whatever they might be called) are meaningful for all
dicts, so they're good candidates for being added to the base dict class.

As for naming, I would suggest reset() instead of set(), to emphasize that the key must be there.
make() is ok; other candidates could be add() or put().

George





More information about the Python-list mailing list