4 ms·
Is it just me or is dict#setdefualt a terribly named function? I just learned of it here, and I was excited that it would set the default value for the dictiona
by basil-rash 8y ago
Is it just me or is dict#setdefualt a terribly named function? I just learned of it here, and I was excited that it would set the default value for the dictionary, when instead it performs something like:
(self, key, default) => self[key] = (key in self ? self[key] : default)
I'd prefer something like "dict#createifnotexists", but better.
- deleted 8y ago[deleted]
- BerislavLopac 8y agoOr get_or_create, to follow Django's example.
- ac29 8y agoAre you maybe looking for a defaultdict? https://docs.python.org/3.7/library/collections.html#collections.defaultdict https://docs.python.org/3.7/library/collections.html#collect...
- basil-rash 8y agoI'm aware of defaultdict, but when I saw that method on dict I got excited because I thought it meant I didn't need defaultdict anymore.