python中return 语句与 分支语句连用问题
迷茫
迷茫 2017-04-18 10:31:56
0
2
920

在如下代码中
return ' '.join(s.split()[::-1]) if s.strip() != "" else s
为什么if s.strip() != "" else s写在return之后,照样可以判断
这条Python语句工作过程是怎么样的,尤其是return语句与分支语句的关系

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
迷茫

In fact, it is the ternary operator in other languages ​​

if s.strip() !== "":
    return ' '.join(s.split()[::-1])
else:
    return s
黄舟

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;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template