子流程returncode中的Python'return not'语句 [英] Python 'return not' statement in subprocess returncode

查看:887
本文介绍了子流程returncode中的Python'return not'语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了Python中非常奇怪的代码行:

I just came across a very strange line of code in Python:

....
self.myReturnCode = externalProcessPopen.returncode
....
....
return not self.myReturnCode
....

return not到底代表什么?我知道,Popen进程仍在运行时,其返回码为无",完成并成功退出时为随机数.但是,代码的作者究竟想在这里实现什么?

What exactly return not stands for? I am aware that the returncode of a Popen process is None while it's still running and a random number once it completes and exits successfully. But what exactly is the author of the code trying to achieve here?

也许值得一提的是,稍后同一位作者检查返回代码是这样的:

It might also be worth noting that the same author later on checks the return code like this:

if not testClass.testFunction():
    logger.error('Failed to execute Function')
    ....

推荐答案

not布尔运算符,该布尔运算符返回该值的布尔值倒数. return返回该运算符的结果.换句话说,该表达式应读取为return (not self.myReturnCode).引用文档:

not is a boolean operator that returns the boolean inverse of the value. return returns the result of that operator. In other words, the expression should be read as return (not self.myReturnCode). Quoting the documentation:

如果运算符not的参数为false,则产生True,否则为False.

The operator not yields True if its argument is false, False otherwise.

如果self.myReturnCode为真值,则not self.myReturnCodeFalse,反之亦然.请注意,self.myReturnCode可以是任何Python值,但是not 始终会返回布尔值,即TrueFalse.

If self.myReturnCode is a true value, not self.myReturnCode is False, and vice versa. Note that self.myReturnCode can be any Python value, but not always returns a boolean value, either True or False.

如果externalProcessPopen.returncode是外部进程的返回代码,则如果该进程因错误退出而将是一个正整数,如果0成功退出则将是一个正整数.这称为流程退出状态;返回的非零值完全取决于过程. not 0Truenot 1(或更高的整数值)为您False.

If externalProcessPopen.returncode is the return code of an external process, then it'll be a positive integer if the process exited with an error, 0 if it exited successfully. This is called the process exit status; what non-zero values are returned depends entirely on the process. not 0 is then True, not 1 (or a higher integer value) gives you False.

如果它是None,则也会返回True(not NoneTrue),但是如果subprocess.Popen()返回代码仅 None程序尚未退出.

If it is None, then True (not None is True) would be returned as well, but a subprocess.Popen() return code is only None if the process has not yet exited.

这篇关于子流程returncode中的Python'return not'语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆