missing? dictionary methods

Terry Reedy tjreedy at udel.edu
Mon Mar 21 20:22:07 CET 2005


"Antoon Pardon" <apardon at forel.vub.ac.be> wrote in message 
news:slrnd3t10j.20i.apardon at rcpc42.vub.ac.be...
> 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?

To me, one of the major problems with OOP is that there are an unbounded 
number of functions that we can think of to operate on a date structure and 
thus a continual pressure to turn functions into methods and thus 
indefinitely expand a data structure class.  And whatever is the least used 
current method, there will always be candidates which are arguably at least 
or almost as useful.  And the addition of one method will be seen as reason 
to add another, and another, and another.  I was almost opposed to .get for 
this reason.  I think dict has about enough 'basic' methods.

So, without suppost from many people, your two examples strike me as fairly 
specialized usages best written, as easily done, as Python functions.

Terry J. Reedy






More information about the Python-list mailing list