defaultdict's bug or feature?

Red Forks redforks at
Thu May 21 14:07:50 CEST 2009

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?

I think dict.get() method is just a *safe* version of dict[key], maybe it
should be:

def get(self, key, default = None):
    return self[key]
  except KeyError:
    return default
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list