defaultdict's bug or feature?

Red Forks redforks at gmail.com
Thu May 21 20:44:37 EDT 2009


You mean 'get' method should not alter the dict, does 'dict[key]' should not
alter the dict either?

d = defaultdict(set)
assert len(d) == 0
print d[1]
assert len(d) == 1

auto insert value to dict, when value is not in dict, is what defaultdict
try to do.

On Fri, May 22, 2009 at 7:46 AM, Rhodri James
<rhodri at wildebst.demon.co.uk>wrote:

> On Thu, 21 May 2009 13:07:50 +0100, Red Forks <redforks at gmail.com> wrote:
>
>  from collections import defaultdict
>>
>> d = defaultdict(set)
>> assert isinstance(d['a'], set)
>> assert isinstance(d.get('b'), set)
>>
>> d['a'] is ok, and a new set object is insert to d, but d.get('b') won't.
>>
>> It's a bug, or just a feature?
>>
>
> Feature.  You're blaming 'get' for doing exactly what it said it would,
> both in returning None and not gratuitously altering the dictionary.
>
> --
> Rhodri James *-* Wildebeeste Herder to the Masses
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090522/e4539a1a/attachment.html>


More information about the Python-list mailing list