kst*_*tis 3 python subprocess return popen return-code
我在Python中遇到了一段非常奇怪的代码:
....
self.myReturnCode = externalProcessPopen.returncode
....
....
return not self.myReturnCode
....
Run Code Online (Sandbox Code Playgroud)
究竟return not代表什么?我知道Popen进程的返回码在它仍在运行时为None,一旦完成并成功退出,则为随机数.但是代码的作者究竟想要在这里实现什么呢?
也许值得注意的是,同一作者稍后会检查返回代码,如下所示:
if not testClass.testFunction():
logger.error('Failed to execute Function')
....
Run Code Online (Sandbox Code Playgroud)
not是一个布尔运算符,它返回值的布尔反转.return返回该运算符的结果.换句话说,表达式应该被理解为return (not self.myReturnCode).引用文档:
如果运算符的参数为false,则运算符
not产生.TrueFalse
如果self.myReturnCode是真值,not self.myReturnCode则是False,反之亦然.请注意,self.myReturnCode可以是任何Python值,但not 始终返回布尔值,True或者False.
如果externalProcessPopen.returncode是外部进程的返回代码,那么如果进程退出并出错,0则它将是一个正整数,如果它成功退出.这称为流程退出状态 ; 返回的非零值完全取决于进程.not 0然后True,not 1(或更高的整数值)给你False.
如果是None,则返回True(not None是True),但subprocess.Popen()返回代码仅 None在进程尚未退出时返回.
return not self.myReturnCode
Run Code Online (Sandbox Code Playgroud)
应解释为:
return (not self.myReturnCode)
Run Code Online (Sandbox Code Playgroud)
它在你的代码中做的只是这样:
0则返回True0那么返回False.| 归档时间: |
|
| 查看次数: |
8417 次 |
| 最近记录: |