带有 or 语句的 python 表达式

标签 python

我在 github 链接中看到下面的语句,想知道这个条件是如何执行的。 or 的右侧表达式检查条件,但左侧更像是一个赋值语句。

https://github.com/wkentaro/labelme/blob/30a29a4a9c1f355cdbd69e65a0f3aad6948f458d/labelme/canvas.py#L464

shape.fill = shape.selected or shape == self.hShape

最佳答案

在此上下文中,shape.selected 是一个 bool 属性,因此代码等同于:

if shape.selected:
    shape.fill = shape.selected
else:
    shape.fill = shape == self.hShape  

这有帮助吗?

关于带有 or 语句的 python 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51162074/

相关文章:

python - 无法加载 dynlib/dll -\\bin\\libeay32'。当应用程序被卡住时,很可能没有找到这个 dynlib/dll

python - 通过 Python 将股票数据下载到 excel(缺少日期列)

python - Boto3 - 等待 AWS 数据库迁移任务被删除

python - Pycharm调试器无法启动: finished with exit code -1073741819 (0xC0000005)

python - 使用 Python 将 JSON 数据插入 SQL Server

python - 玩家在预定路径上行走pygame

python - 多次调用 pd.DataFrame.plot() 后颜色一致

python - 将 python 字节类型更改为字符串

python - boto3 : execute_command inside python script

python - PyCharm & Pyenv 本地?