0

I'm looking to find a good convention on returning fail from a function if the function fails. I typically like to return None or False but in my case, the function's purpose is to read an IO, which could be bool (True/False), int or float.

So in this case I can't return a False. I've tried to use None but I don't know if this is the best case cause if I don't check the return as I call the function the None output might be recognized as a False output.

I was thinking having a definition files that has string tokens, but that seems in efficient to have to parse the string.

Are there built-in objects available? Whats the convention?

4
  • 3
    Pretty sure the convention is to raise an error. Commented Jul 21, 2016 at 19:54
  • 6
    Have you considered raising an exception on error? Commented Jul 21, 2016 at 19:54
  • 1
    Use exceptions. That's what they're for. I know a lot of programmers who aren't familiar with them feel like they don't want an "error" to happen at all, and that simply catching it isn't a great solution, but it's not an error, it's an exception - and that's what they're there for. Commented Jul 21, 2016 at 19:54
  • If you want something that doesn't just get hidden when you forget to check the return value, you pretty much have to raise an exception. Commented Jul 21, 2016 at 19:55

1 Answer 1

1

You should raise an exception if the function fails. It is not good practice to have to check if your return value is invalid per the EAFP principle of Python.

Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.

Sign up to request clarification or add additional context in comments.

4 Comments

If I were to raise an exception in the function, wouldn't I still need to return something from the function and handle it from the caller side so that my script continues to run? Or should I have another try and exception statement from the caller side?
The exceptions idea is briefly to separate two programs flows: normal (if everything is ok) and exceptional (when something is broken). When exceptions are appeared in program languages, old "check return value" programming style was deprecated (with rare exceptions) for languages which support exceptions mechanisms. When your function finishes normally, it returns expected result value. When not - it raises exception, and caller module should catch it and process it - if it knows what to do with this exception, return value will not be transferred to a caller function.
@lospejos For the caller module to "catch it and process it" what would you do? Would you use a try and except statement to handle it?
@Sam Personally I'm guiding next relatively simple rule: if I can get exception from underlying method/function, then I catch it in two cases: when I need write to log and when I can what I will do with that exception. Otherwise (f.e. I've got the exception, but I cannot do anything) I just rethrow it upper to call stack (just not put try.. except blocks when call a function). Also when I log exception and cannot handle it (don't know what to do) - I also rethrow it upper to call stack. Also never hide exceptions (empty catch block) unless you're absolutely sure what do you do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.