| ▲ | saint-evan 10 hours ago | |||||||||||||||||||||||||
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? | ||||||||||||||||||||||||||
| ▲ | beecasthurlbow 10 hours ago | parent | next [-] | |||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||
| ▲ | sirsinsalot 5 hours ago | parent | prev [-] | |||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||