raise statement on a conditional expression(raise 条件表达式上的语句)
问题描述
遵循武士原则",我正在尝试在我的功能上执行此操作,但似乎是错误的......
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
是表达式,而不是语句.您的尝试意味着如果 bool,返回值,否则返回 raise 表达式
的结果" - 这当然是无稽之谈,因为 raise exception
本身就是一个语句而不是表达式.
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
这篇关于raise 条件表达式上的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:raise 条件表达式上的语句


基础教程推荐
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01