I just came across a very strange line of code in Python:
....
self.myReturnCode = externalProcessPopen.returncode
....
....
return not self.myReturnCode
....
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')
....
notis a boolean operator that returns the boolean inverse of the value.returnreturns the result of that operator. In other words, the expression should be read asreturn (not self.myReturnCode). Quoting the documentation:If
self.myReturnCodeis a true value,not self.myReturnCodeisFalse, and vice versa. Note thatself.myReturnCodecan be any Python value, butnotalways returns a boolean value, eitherTrueorFalse.If
externalProcessPopen.returncodeis the return code of an external process, then it'll be a positive integer if the process exited with an error,0if it exited successfully. This is called the process exit status; what non-zero values are returned depends entirely on the process.not 0is thenTrue,not 1(or a higher integer value) gives youFalse.If it is
None, thenTrue(not NoneisTrue) would be returned as well, but asubprocess.Popen()return code is onlyNoneif the process has not yet exited.