views:

53

answers:

3
errors = {}

#errorexample
errors['id'] += ('error1',)
errors['id'] += ('error2',)

#works but ugly
errors['id'] = ('error1',)
errors['id'] += ('error2',)

If 'error1' is not present it will fail. Do I really have to extend dict?

+3  A: 
import collections
errors = collections.defaultdict(tuple)
liori
+4  A: 

Use a collections.defaultdict instead of a plain dict -- this kind of convenience, after all, is exactly what the default-dict type was introduced for:

>>> import collections
>>> errors = collections.defaultdict(tuple)
>>> errors['id'] += ('error1',)
>>> errors['id'] += ('error2',)
>>> errors['id']
('error1', 'error2')
Alex Martelli
great thx :) python keeps me DRY :)
mo
+1  A: 
>>> from collections import defaultdict
>>> errors = defaultdict (tuple)
>>> errors['id'] += ('blargh',)
>>> errors['id']
('blargh',)
doublep