Python Worst Practices

Chris Angelico rosuav at gmail.com
Wed Feb 25 18:57:20 EST 2015


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? Isn't it much more likely that, for instance, 0
>> means success and nonzero means error (and maybe there's just one
>> error state, so 1 means failure)?)
>
> You've answered your question, I believe. In some contexts – such as
> Unix shell – 0 ⇒ success ⇒ true, and non-0 ⇒ failure ⇒ 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). Instead, use "success" and
"failure". That is to say: The function doesn't return a boolean for
whether it did something or not, it returns an error code (or 0 if no
error). I agree with the presentation, but have never seen code that
does this; it's like saying "Python worst practice: Defining your
integer constants using Shakespeare syntax. Python best practice:
Defining your integer constants as decimal or hexadecimal digits.", in
that nobody would ever do the worst-practice side of it. But maybe
I've lived a sheltered life...

ChrisA



More information about the Python-list mailing list