Python Worst Practices

Simon Ward simon+python at bleah.co.uk
Fri Feb 27 15:06:25 EST 2015



On 26 February 2015 21:23:34 GMT+00:00, Ben Finney <ben+python at benfinney.id.au> wrote:
>Simon Ward <simon+python at bleah.co.uk> writes:

>> 0 = success and non-zero = failure is the meme established, rather
>> than 0 = true, non-zero = false.
>
>That is not the case: the commands ‘true’ (returns value 0) and ‘false’
>(returns value 1) are long established in Unix. So that *is* part of
>the
>meme I'm describing.

I mentioned the true and false. OK, so it's a meme, but it's based on a false (pun intended) understanding of exit status codes. That success evaluates to true and failure evaluates to false does not mean the values of truth and falseness are inverted. No programming language other than that provided by system shells I have used evaluates 0 to true.

The shell case is unique because it typically runs processes and has to deal with exit status codes. It feels quite natural to me that, if COMMAND completes successfully (exit code 0) then writing "if COMMAND ; then : true bit ; else : false bit ; fi" should execute the true bit, but that doesn't mean all boolean logic is inverted.

true is define to return 0, or no error. false is defined to return 1, or error. They are just commands though, and the exit codes are still exit codes, not boolean values.

>> None of the above is a good reason to use error *or* success return
>> values in Python--use exceptions!--but may be encountered when
>running
>> other processes.
>
>Right. But likewise, don't deny that “true == 0” and “false ==
>non-zero”
>has a wide acceptance in the programming community too.

I can't deny that, but I can state what things really mean and point out why everybody is doing it wrong.

Simon



More information about the Python-list mailing list