3 ms·
This is a simple class but has been a real time saver of not having to check if keys exist all the time. I use it quite a bit for metadata structures with opti
by prepend 6y ago
This is a simple class but has been a real time saver of not having to check if keys exist all the time.
I use it quite a bit for metadata structures with optional elements so reads will still give something back, even if an empty string or a default value.
- WhyCause 6y agoYou do know about `.get()`, right? if `a = {'a': 1, 'b': 2}`, and I do `cval = a.get('c', 3)`, `cval` will contain 3. If I do `cval = a['c']`, I get an exception
- prepend 6y agoYes, I think this is how default dict is implemented. The issue here is that the clients have to know the particular default values for each parameter. For me, it was easier to return a defaultdict where the callers didn’t need to know what default value to pass into their gets.