Python Return Codes
python, styles
Solution
Don't return something to indicate an error. Throw an exception. Definitely don't catch an exception and turn it into a return code.
Problem
So lately I've been asking a few questions about more professional and pythonic style in Python, and despite being given great answers to my questions, I feel like I need to ask a much broader question. In general, when writing utility functions (for a library, etc.) that deal more with side effects (file writes, dictionary definitions, etc.) than return values, it's very useful to return a status code to tell the calling function that it passed or failed. In Python, there seem to be three ways to flag this: Using a return value of -1 or 0 (C like) and using statements such as ``` if my_function(args) < 0: fail condition pass condition ``` or using a return value of True/False ``` if not my_function(args): fail condition pass condition ``` or using a 'return or 'return None' using exceptions (exits on unknown error) ``` try: my_function(args) except ExpectedOrKnownExceptionOrError: fail condition pass condition ``` Which of these is best? Most correct? Preferred? I understand all work, and there isn't much technical advantage of one over the other (except perhaps the overhead of exception handling).