The expression x if C else y first evaluates the condition, C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.
因此,您的具体示例相当于:
if color == self.fill2:
color = self.fill1
else:
color = self.fill2
这是一个conditional expression见PEP-308。你知道吗
像这样的事情
也可以写成
这被称为conditional expression。你知道吗
因此,您的具体示例相当于:
这不是列表理解。它是一种句法上的糖分。 讽刺的是,这是为了提高可读性。你知道吗
可以解释为:
相关问题 更多 >
编程相关推荐