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?
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?
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')
>>> from collections import defaultdict
>>> errors = defaultdict (tuple)
>>> errors['id'] += ('blargh',)
>>> errors['id']
('blargh',)