Python 'return not' statement in subprocess returncode

穿精又带淫゛_ 提交于 2019-12-06 02:54:09

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:

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

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.

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.

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.

return not self.myReturnCode

is to be interpreted as:

return (not self.myReturnCode)

What it is doing in your code is simply this:

  • If the returncode is 0 then return True
  • If the returncode is not 0 then return False.

It wouldn't be a random number, it's the return code of the external process, where zero indicates success and a non-zero number indicates failure.

Hence returning not self.myReturnCode means it returns True when the process was successful and False when the process indicated failure.

return not self.myReturnCode

is equivalent to:

return False if self.myReturnCode else True
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!