Whatdoesa,operatordowhenusedintherighthandsideofaconditional?
a = 10
b = 20
c = 30
if(a > b,c):
print('In if')
else:
print('In else')
Someone posted the above piece of code, and asked why the above always results in 'In if', being printed regardless of the values of b and c.
虽然这看起来像是糟糕的编程风格,但我很好奇,操作员在做什么。
到目前为止,我还没有在文档中找到答案。任何人都可以提供解释吗?
回答
a > b, c是元组((a > b), c)。
所以如果a=10, b=20, c=30,那么我们问的是元组(False, 30)是否为真。所有非空元组都是真实的,因此这将始终通过条件触发相同的路径。
THE END
二维码