22 ms·
That's why in python that raises a KeyError, or if you use explicitly .get() you can specify anything as your default value.
by bombolo 3y ago
That's why in python that raises a KeyError, or if you use explicitly .get() you can specify anything as your default value.
- codebje 3y agoIt's probably subjective, but I don't see the need to wrap your code in a try/except block as an improvement over an optional sum type, and I really don't see the use of a magic value to indicate no value as an improvement over anything.
- bombolo 3y agoIf it's not a try it's an if... you still need to handle the case where the key wasn't present and figure something out.
- lmm 3y agoWhich is why you end up with the painful sentinel = object() value = dictionary.get(key, sentinel) if(value == sentinel) ... Which, sure, it works, but it's working around a problem that didn't need to exist in the first place. Why not just have Option work the way you expect, and be able to contain None the same way as it can contain any other value?