Python Worst Practices

Chris Angelico rosuav at gmail.com
Wed Feb 25 19:37:31 EST 2015


On Thu, Feb 26, 2015 at 11:11 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>
>> On Thu, Feb 26, 2015 at 10:48 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
>> > Chris Angelico <rosuav at gmail.com> writes:
>> >
>> >> (Flipping the booleans makes no sense to me. When would 0 mean true
>> >> and 1 mean false? […])
>> >
>> > The Unix commands ‘true’ and ‘false’ follow that convention
>> > <URL:https://en.wikipedia.org/wiki/True_and_false_%28commands%29>.
>>
>> Yes, but my point is: You shouldn't need to rebind those names (or
>> have names "true" and "false" for 0 and 1).
>
> That's not what you asked, though. You asked “When would 0 mean true and
> 1 mean false?” My answer: in all Unix shell contexts.

My bad. I was talking in a context of Python programming, specifically
with APIs where you would use some kind of true/false flag as either a
function parameter or a return value. There are plenty of contexts
where you want a success/failure response, but you can name your
constants "success" and "failure" for that, rather than "true" and
"false".

>> Instead, use "success" and "failure".
>
> You'd better borrow the time machine and tell the creators of Unix. The
> meme is already established for decades now.

IMO the whole system of boolean logic in shell scripts is a massive
pile of hacks. In its purest sense, you have "do this, and if it
succeeds, do that":

cp source destination && rm source

which makes reasonable sense with an "and" operator - do this, and do
this - and the converse makes similar sense with an "or" operator:

cp source destination || echo Oopsie

On the assumption that a nonzero return code from cp means it didn't
copy the file, this reads as "copy the file or emit that message",
which is exactly right. Nice. But that's stretching the notion of
"and" and "or" a little, to the point where booleans and comparisons
stop making so much sense:

[ -f /usr/local/bin/python ] || sudo make install

Is that a success/failure, or is it an if-then? Kinda neither, kinda
both, kinda one built on the other built on the other.

cp source destination || true

Now this makes no sense outside of its very specific purpose. "Or
true"? When would you use that? What it *actually* means is "attempt
this copy, and if it fails, pretend it succeeded" (presumably because
you have something aborting on error, like "set -e"). It's not boolean
logic, and it's not really an alternate command for the failure case.

The sh and bash system of logic works, there's no doubt about it. It
takes "practicality beats purity" a very long way, and makes something
where a fairly simple construct ("||" just means "if nonzero error
code, execute this, otherwise don't") can do three or four
barely-related jobs, which makes the language more compact. But none
of this means that Python should, in any way, imitate it.

ChrisA



More information about the Python-list mailing list