How do I elegantly implement the "Samurai principle" (return victorious, or not at all) on my functions?
return <value> if <bool> else raise <exception>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Inline/ternary
ifis an expression, not a statement. Your attempt means “if bool, return value, else return the result ofraise expression” – which is nonsense of course, becauseraise exceptionis itself a statement not an expression.There’s no way to do this inline, and you shouldn’t want to. Do it explicitly: