missing? dictionary methods

Antoon Pardon apardon at forel.vub.ac.be
Tue Mar 22 02:40:50 EST 2005


Op 2005-03-21, Terry Reedy schreef <tjreedy at udel.edu>:
>
> "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.

I don't know it they are so specialized. I would rather say the
map[key] = value semantics is specialized. If we work with a list
the key already has to exist. If you have a list with 4 elements
and you try to assign to the 6th element you get an IndexError.
If you want to assign to the 6th element you have to construct
that first. That and for symetric reason with var = dct[key]
make me think that dct[key] = value shouldn't just construct
an entry when it isn't present.

I also was under the impression that a particular part of
my program almost doubled in execution time once I replaced
the naive dictionary assignment with these self implemented
methods. A rather heavy burden IMO for something that would
require almost no extra burden when implemented as a built-in.

But you are right that there doesn't seem to be much support
for this. So I won't press the matter.

-- 
Antoon Pardon



More information about the Python-list mailing list