亲爱的 C 程序员,很抱歉向您提出这样一个基本的问题,但是这相当于什么:
val = dim == 0 ? 1 : S(dim) * pow(radius * 0.5, dim) / dim
在 Python 中?我正在包装一些 C 代码,我需要知道这一行中发生了什么,以便与 Python 结果进行比较...
最佳答案
Python Conditional Expression :
从 Python 2.5 开始,条件表达式 - a ? b : c
可以这样写:
b if a else c
因此,您的表达式将等同于:
val = 1 if dim == 0 else S(dim) * pow(radius * 0.5, dim) / dim
关于C 表达式的 Python 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422053/