Python : 了解参数中两个对象的逻辑或
Python : Understanding logical or of two object in parameter
在this python code line 256 to 266
这个用run in sarge
p = run(
cmd,
input=self.feeder,
async_=True,
stdout=self.stdout or Capture(timeout=0.1, buffer_size=1),
stderr=self.stderr or Capture(timeout=0.1, buffer_size=1),
cwd=self.cwd,
env=self.env,
shell=self.shell,
**run_args
)
我很难理解 OR 逻辑运算符 stdout=self.stdout or Capture(timeout=0.1, buffer_size=1)
这个OR
参数中的两个对象?
如果stdout=self.stdout
是true并且Capture(timeout=0.1, buffer_size=1)
是true,那么整个语句是真.
如果stdout=self.stdout
是true并且Capture(timeout=0.1, buffer_size=1)
是false,那么整个语句是true,反之亦然
如果stdout=self.stdout
是false并且Capture(timeout=0.1, buffer_size=1)
是false,那么整个语句是假.
它将使用第一个“truthy”变量。在您的示例中,如果 self.stdout 为 null,它将分配给 Capture(timeout=0.1, buffer_size=1)。
这是一个很棒的资源 https://realpython.com/python-or-operator/
查看有关“非布尔上下文”的部分
据我了解,如果第一个语句不可用 (None),当您想为参数(第二个语句)指定默认值时使用 OR
。
在您的情况下,如果 self.stdout=None
,stdout
将具有第二条语句 (Capture(timeout=0.1, buffer_size=1)
) 的值。
在this python code line 256 to 266
这个用run in sarge
p = run(
cmd,
input=self.feeder,
async_=True,
stdout=self.stdout or Capture(timeout=0.1, buffer_size=1),
stderr=self.stderr or Capture(timeout=0.1, buffer_size=1),
cwd=self.cwd,
env=self.env,
shell=self.shell,
**run_args
)
我很难理解 OR 逻辑运算符 stdout=self.stdout or Capture(timeout=0.1, buffer_size=1)
这个OR
参数中的两个对象?
如果stdout=self.stdout
是true并且Capture(timeout=0.1, buffer_size=1)
是true,那么整个语句是真.
如果stdout=self.stdout
是true并且Capture(timeout=0.1, buffer_size=1)
是false,那么整个语句是true,反之亦然
如果stdout=self.stdout
是false并且Capture(timeout=0.1, buffer_size=1)
是false,那么整个语句是假.
它将使用第一个“truthy”变量。在您的示例中,如果 self.stdout 为 null,它将分配给 Capture(timeout=0.1, buffer_size=1)。 这是一个很棒的资源 https://realpython.com/python-or-operator/ 查看有关“非布尔上下文”的部分
据我了解,如果第一个语句不可用 (None),当您想为参数(第二个语句)指定默认值时使用 OR
。
在您的情况下,如果 self.stdout=None
,stdout
将具有第二条语句 (Capture(timeout=0.1, buffer_size=1)
) 的值。