I am using a Python based API where there are lots of functions to query things, like doesPointExist
, findPoint
, canCreateNewPoint
, etc where the negative result throws an exception. This makes the code much more cluttered filled with try/catch statements, instead of directly using the result as a boolean value.
Since I am not a Python expert, I am wondering if this design is Pythonic or not? I haven't seen this sort of design in the standard libraries though, so I am assuming this kind of exception usage in Python APIs is frowned upon?
API design is a bit of an art. The name of a function should suggest how it will behave, including setting up user expectations. A function named findPoint
implies that the search may fail, so the case where no such point exists is not exceptional, and may return None
to signal the result. A function named getPoint
, however, would imply to me that it can always return the requested point. A failure would be unexpected and warrant raising an exception.