Following "Samurai principle", I'm trying to do this on my functions but seems it's wrong...
return <value> if <bool> else raise <exception>
Is there any other "beautiful" way to do this? Thanks
Following "Samurai principle", I'm trying to do this on my functions but seems it's wrong...
return <value> if <bool> else raise <exception>
Is there any other "beautiful" way to do this? Thanks
If you absolutely want to
raise
in an expression, you could doThis "tries" to return the return value of
raiser()
, which would beNone
, if there was no unconditionalraise
in the function.I like to do it with assertions, so you emphasize that that member must to be, like a contract.
Inline/ternary
if
is an expression, not a statement. Your attempt means "if bool, return value, else return the result ofraise expression
" - which is nonsense of course, becauseraise exception
is itself a statement not an expression.There's no way to do this inline, and you shouldn't want to. Do it explicitly:
Well, you could test for the bool separately:
That way, you could test for
expr
earlier.