简体   繁体   English

方括号内带有`or`的python语句

[英]python statement with `or` inside square brackets

This is a code snippet I pasted from a setup.py python file.这是我从 setup.py python 文件粘贴的代码片段。 I am new to python and don't understand this build_args variable.我是 python 新手,不理解这个build_args变量。 Could someone give me some explanation for that?有人可以给我一些解释吗?

build_args = [NINJA or MAKE]

# control the number of concurrent jobs
if self.jobs is not None:
    build_args.extend(['-j', str(self.jobs)])

subprocess.check_call(build_args)

build_args' instantiation is simply evaluating a logical boolean OR statement inside of the list a list structure. build_args 的实例化只是评估列表结构中的逻辑布尔 OR 语句 After the OR statement has been evaluated there will simply be a single boolean value stored in build_args.在 OR 语句被评估后,build_args 中将只存储一个布尔值。 (props to ukemi, beat me to the punch) (给 ukemi 的道具,打我一拳)

Since it was also included in the code snippet I'd add that info for '.extend()' following can be found here .由于它也包含在代码片段中,我会在此处添加“.extend()”的信息 Essentially .extend() then just appends all items of an iterable structure on the end of the list, so build_args' content would then be [<boolean>, '-j', <job_string>]本质上 .extend() 然后只是将可迭代结构的所有项目附加到列表的末尾,因此 build_args' 内容将是[<boolean>, '-j', <job_string>]

Use:用:

build_args = [NINJA or MAKE]
  • If NINJA is "truthy", then build_args = [NINJA]如果NINJA是“真实的”,那么build_args = [NINJA]
  • If NINJA is "falsy", then build_args = [MAKE]如果NINJA是“falsy”,则build_args = [MAKE]


Note: Python's Truthy and Falsy - generalized Booleans注意:Python 的 Truthy 和 Falsy - 广义布尔值

In python there are other values equivalent to True and False , other than the booleans themselves:在 python 中,除了布尔值本身之外,还有其他等效于TrueFalse的值:

In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false:在布尔运算的上下文中,以及当控制流语句使用表达式时,以下值被解释为 false:

  • False
  • None
  • numeric zero of all types所有类型的数字零
  • empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets)空字符串和容器(包括字符串、元组、列表、字典、集合和冻结集)

All other values are interpreted as true.所有其他值都被解释为真。


Truth table for or : or真值表:

x X y x or y x or y
True真的 True真的 x X
True真的 False错误的 x X
False错误的 True真的 y
False错误的 False错误的 y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM