在如下代码中
return ' '.join(s.split()[::-1]) if s.strip() != "" else s
为什么if s.strip() != "" else s写在return之后,照样可以判断
这条Python语句工作过程是怎么样的,尤其是return语句与分支语句的关系
迷茫2017-04-18 10:33:56
In fact, it is the ternary operator in other languages
if s.strip() !== "":
return ' '.join(s.split()[::-1])
else:
return s
黄舟2017-04-18 10:33:56
Return is followed by a whole. The boss above made it very clear, it’s the ternary operator
' '.join(s.split()[::-1]) if s.strip() != "" else s
# 简化版
A if X else B
If X is True, then the overall value is A, otherwise it is B
This is how the ternary operator is written in other languages
X ? A : B;