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.stdouttrue并且Capture(timeout=0.1, buffer_size=1)true,那么整个语句是.

如果stdout=self.stdouttrue并且Capture(timeout=0.1, buffer_size=1)false,那么整个语句是true,反之亦然

如果stdout=self.stdoutfalse并且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=Nonestdout 将具有第二条语句 (Capture(timeout=0.1, buffer_size=1)) 的值。