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.
Sounds like a badly designed API.
doesPointExist
should return True
or False
, it shouldn't raise an exception when the point doesn't exist.findPoint
should return a Point
object or None
when no object could be found.canCreateNewPoint
should return True
or False
for similar reasons.Exceptions are for exceptional cases.
I don't agree that you won't find this in the standard library. For example, "abc".index("d")
raises ValueError
, lots of libraries raise exceptions freely.
I'd say it depends on what the consequences of a failed action are.
False
, if it's a yes or no question).findPoint()
might do that if it normally returns a Point
object that the caller wants to work with.1, I would recommend to use exception for very specific cases, so you do not need to test for them in common situations.
2, You should read guidelines PEP8 Naming Conventions regarding the names of your functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With