Hacker News new | past | comments | ask | show | jobs | submit
I don't get it... What's the problem? I too prefer getting None as opposed to a key Error which would prolly necessitate a try..catch as opposed just checking for None. I'm having trouble reasoning up a situation where raising a keyError would make any sense over a None for data structures like dicts that could return a value or no value! Help?
KeyError localizes the issue to the lookup (ie “right here this key should be present and it’s not) vs later in the code where it can be hard to trace where the None value came from or how to handle it.
Well... yeah but at the cost of halting your entire program? As opposed to logging?
loading story #50235851
{"deleted":true,"id":50240616,"parent":50235250,"time":1793002057,"type":"comment"}
Or you know, test if the key exists before using it. The key could exist and be None, representing a legitimate None at that key. Or the key might not exist and asking for it is a bug.

Use a defaultdict if you must have all keys optional. Using get() everywhere is buggy because it masks illegal key use.

> test if the key exists

Double lookup.

Cheaper than fixing bugs due to illegal key access.

Cheaper than get() masking bugs in your code or dict generation.

But yes let's prematurely optimise for speed in a slow-by-default language by writing bug prone non-idiomatic code. OK.