Python 'return not' statement in subprocess returncode
Asked Answered
C

4

5

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')
    ....
Cetology answered 30/12, 2013 at 12:43 Comment(0)
B
10

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.

Babettebabeuf answered 30/12, 2013 at 12:45 Comment(6)
It seems you are suggesting that 0 is not a true value (why?) while 1 or higher integer values are true values. This begs the question what is the definition of a 'true value'?Cetology
@Konos5: don't confuse process exit status convention with Python booleans. The not translates one convention to another; 0 means success for process, translated to True here.Babettebabeuf
Yes, I get that. 0 is success for the process but it seems that for Python is not a true value - that's why we use not. My question is why is 0 not considered a true value in the first place..Cetology
Numeric 0, empty containers and empty strings are all considered false in Python. This makes for very readable code in many situations. Why do you feel 0 should be true instead?Babettebabeuf
And historically, booleans in Python are relatively new. In C, 0 is false as well, and Python used integers instead of booleans for a long time. Today, bool is a subclass of int and True == 1 and False == 0 are both true.Babettebabeuf
I was a bit confused but now totally covered. Thank you.Cetology
I
6
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.
Ironbound answered 30/12, 2013 at 12:45 Comment(0)
A
3
return not self.myReturnCode

is equivalent to:

return False if self.myReturnCode else True
America answered 30/12, 2013 at 12:51 Comment(0)
S
2

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.

Speedometer answered 30/12, 2013 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.