defaultdict's bug or feature?

Rhodri James rhodri at
Fri May 22 06:35:39 CEST 2009

Please don't top-post, it makes the thread of argument hard to follow.

On Fri, 22 May 2009 01:44:37 +0100, Red Forks <redforks at> wrote:

> 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.

Behaviour you are deliberately avoiding by calling `get`, since that
explicitly behaves the same way that it does for dicts.  You're doing
two different things through two different interfaces with two different
specs.  Why are you expecting them to have the same effect?

Rhodri James *-* Wildebeeste Herder to the Masses

More information about the Python-list mailing list