logoalt Hacker News

RantyDave • today at 8:16 PM • 3 replies • view on HN

"A simple cache returns None both for a missing key and for a key that maps to None."

No, it returns KeyError. Or at least it should.


Replies

0cf8612b2e1e • today at 9:19 PM

The author’s example chose to use the syntax that returns None on missing. If they wanted clarity the standard API does raise.

  cache = {“key”: “value”, “some input”: None}
  cache.get(“missing_key”)
Vs

  cache[“missing_key”]
There are better examples that could have been highlighted.
travisd • today at 8:42 PM

it’s maybe less “pythonic” but at $dayjob we’ve very actively moved away from exceptions for things in favor of things that are statically type checked. too many things that become runtime bugs instead of type checking bugs.

with match now being available, we even return explicit error objects and the ergonomics are pretty good (reminds me of rust).

➕ show 1 reply
lyorig • today at 8:26 PM

Perhaps the author has had their fair share of run-ins with libraries that didn't think their API through like that. But yes, they should.

➕ show 1 reply