Hacker News new | ask | show | jobs
by basil-rash 2689 days 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.
2 comments

I'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.
Or get_or_create, to follow Django's example.