在条件表达式上引发语句 [英] raise statement on a conditional expression

查看:114
本文介绍了在条件表达式上引发语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循武士原则,我试图在我的函数上执行此操作,但似乎是错误的...

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

推荐答案

内联/三元如果是表达式,而不是语句。您的尝试表示如果bool,则返回值,否则返回 raise表达式的结果-当然这是无稽之谈,因为 raise例外本身是语句,而不是表达式。

Inline/ternary if is an expression, not a statement. Your attempt means "if bool, return value, else return the result of raise expression" - which is nonsense of course, because raise 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:

if not bool:
    raise MyException
return value

这篇关于在条件表达式上引发语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆