Pre-PEP: Dictionary accumulator methods
Brian van den Broek
bvande at po-box.mcgill.ca
Sat Mar 19 12:31:16 EST 2005
Kent Johnson said unto the world upon 2005-03-19 07:19:
> Brian van den Broek wrote:
>
>> Raymond Hettinger said unto the world upon 2005-03-18 20:24:
>>
>>> I would like to get everyone's thoughts on two new dictionary methods:
>>>
>>> def appendlist(self, key, *values):
>>> try:
>>> self[key].extend(values)
>>> except KeyError:
>>> self[key] = list(values)
>>>
>> For appendlist, I would have expected
>>
>> def appendlist(self, key, sequence):
>> try:
>> self[key].extend(sequence)
>> except KeyError:
>> self[key] = list(sequence)
>
>
> The original proposal reads better at the point of call when values is a
> single item. In my experience this will be the typical usage:
> d.appendlist(key, 'some value')
>
> as opposed to your proposal which has to be written
> d.appendlist(key, ['some value'])
>
> The original allows values to be a sequence using
> d.appendlist(key, *value_list)
>
> Kent
Right. I did try the alternatives out and get the issue you point to.
But:
1) In my own code, cases where I'd use the proposed appendlist method
are typically cases where I'd want to add multiple items that have
already been collected in a sequence. But, since I've little coding
under my belt, I concede that considerations drawn from my experience
are not terribly probative. :-)
2) Much more important, IMHO, is that the method name `appendlist'
really does suggest it's a list that will be appended. Hence my stated
expectation. While it would make the method name longer, given the
original interface Raymond posted, I would find appendtolist more
transparent.
out-of-my-depth-ly y'rs,
Brian vdB
More information about the Python-list
mailing list