Red Forks 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?
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):
try:
return self[key]
except KeyError:
return default
Isn't that what it does already?
With dict you have the choice of whether to raise an exception or return
a default value if the key is missing.
With defaultdict you have the choice of whether to add the value or
return a default value if the key is missing.
Both classes have their uses.
--
http://mail.python.org/mailman/listinfo/python-list