Earlier quoted context omitted.
The Python community generally advocates an "it's easier to ask for forgiveness than permission" coding style. When faced with a condition of the form "if condition a holds, do b, else c", it's very often a better idea to do "let's try b, and do c in case b fails because condition a didn't hold". In this case it's better because you can avoid computing an extra hash of the object in cases where it's already a key of…
Agree with that "try except" is better than "if else" for file handling ("with" is even better). I am not sure if "try except" is really faster than "if else" in some edge cases in a memoization context, as you claim. What I am sure is that in a didactic context, where you want people to understand code, something like: if args not in stored_results: stored_results[args] = fn(*args) return stored_results[args] is muc…
As far as people understanding code - this is a common pattern in python code, try / except should be easy for anyone to understand.